interop

Passing string from C++ to C#

和自甴很熟 提交于 2019-12-01 09:17:17
I am using the PInvoke, reverse PInvoke scheme as described by Thottam R. Sriram http://blogs.msdn.com/b/thottams/archive/2007/06/02/pinvoke-reverse-pinvoke-and-stdcall-cdecl.aspx Everything seems to work well, except for passing a string from C++ to C. ( In Sriram the string is constructed in c# and passed untouched through c++, so the issue is avoided. ) The c# code looks like this class Program { public delegate void callback(string str); public static void callee(string str) { System.Console.WriteLine("Managed: " + str); } static void Main(string[] args) { gpscom_start(new callback(Program

Is it possible to return a LPWSTR from C++ DLL to C# app

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:03:01
问题 The C++ function definition is this __declspec(dllexport) LPWSTR __stdcall GetErrorString(int errCode); And I call it in C# like this [DllImport("DLLTest.dll")] public static extern string GetErrorString(int errCode); static void Main(string[] args) { string result = GetErrorString(5); } I get an unhandled exception of type System.Runtime.InteropServices.SEHException I'm not even sure if it's ok for the C++ DLL to try to return a LPWSTR to C#... Thanks. 回答1: You might want to try something

How can I create a header in a table for each new page with Word interop?

本秂侑毒 提交于 2019-12-01 09:02:35
I am trying to create a table with a header. I want this header to be repeated for each new page that the table takes. How can I do this in C# with Word 2007 Interop? Microsoft.Office.Interop.Word.Table table; /* ... */ table.Rows[1].HeadingFormat = -1; This is what word for me, Looping through each table at the end foreach (Table item in doc.Tables) { item.Rows[1].HeadingFormat = -1; item.ApplyStyleHeadingRows = true; } and setting a style on each table with the property set to create a header on each new t.set_Style(TableStyle); 来源: https://stackoverflow.com/questions/1816957/how-can-i

Disable the Save As Button in Word 2010

强颜欢笑 提交于 2019-12-01 08:40:55
I have the following code that should disable the Save As button in Word 2010. The method below is being called in the Document_Startup event: private void DisableSaveAsButton() { Object MenuBar = 40; Object FileMenu = 1; Object SaveAsButton = 5; var saveAsBtn = this.ThisApplication.CommandBars[MenuBar].Controls[FileMenu].accChild[SaveAsButton] as CommandBarButton; saveAsBtn.Enabled = false; } I am expecting the Save as Button to be grayed out, but it isn't and it still functions. What am I doing wrong? Blade3 I figured it out. I just had to add a Ribbon XML item to the project with the

How to programatically tell if a word document is corrupt?

南笙酒味 提交于 2019-12-01 08:31:58
I've got a little C# application that interops with word converting a bunch of word .doc files into textfiles and for the most part this works fine. However, if the document is currupt then word cannot open the file and a dialog box pops up, which means that I cannot fully automate this conversion process - someone has to watch for the dialogs. Is there a way to test if a word .doc is currupt, without opening it? Perhaps through word interop or maybe through a 3rd party tool. One idea I've had is to spawn a thread that does the conversion and kill it if the process is open for longer than n

How to save *.ppt, *.pptx files as *.wmv using Interop with C#?

馋奶兔 提交于 2019-12-01 08:25:16
问题 I tried to do this with the next code: using Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint; using System.IO; using Microsoft.Office.Interop.PowerPoint; namespace SavePPT { class Program { static void Main(string[] args) { Application app = new PowerPoint.Application(); var pres = app.Presentations; var file = pres.Open(@"C:\Presentation1.pptx", MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse); file.SaveCopyAs(@"C:\presentation1.wmv", PowerPoint

How do I handle a failed DllImport?

两盒软妹~` 提交于 2019-12-01 08:17:33
I'm attempting to write a C# managed class to wrap SHGetKnownFolderPath, so far it works on Vista, but crashes on XP due to not finding the proper function in shell32.dll, as expected. I want to have it set so I can fallback on a (admittedly hacky) solution using System.Environment.GetFolderPath if using XP. (Or, even better, if it can't find the funciton in shell32.) Is there any way to do this other then conditional compilation? My current code looks like: public abstract class KnownFolders { [DllImport("shell32.dll")] private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType

Getting Object Functionality out of C++ code in C#

☆樱花仙子☆ 提交于 2019-12-01 08:16:10
问题 I have have a function in wrote in C++ that calls some functions in a old lib. This function creates some memory makes the calls and destroys the memory. To optimize this I would create an object that would keep the memory allocated until the object was destroyed. However I'm going to be calling this function from C# and don't believe I can export a Class, just functions or variables. My idea is instead this; Think of the DLL as a class and the use local vars inside the scope of the dll to

COM Exception 0x800A11F9 - Cannot activate application

余生长醉 提交于 2019-12-01 07:47:18
问题 I have a C# 2.0 (WinForms) project in which I try to activate word 2003 (word is installed on the system). By using the following code: private void ActivateWord() { this.Activate(); if (m_WordDocument != null) { try { m_WordDocument.Activate(); if (m_WordDocument.Application != null) { m_WordDocument.Application.Visible = true; m_WordDocument.Application.Activate(); } } catch (COMException comEx) { ShowError(this, comEx.Message, false); } } } When my application executes m_WordDocument

Bullet points in Word with c# Interop

馋奶兔 提交于 2019-12-01 07:34:39
I have the following code which is supposed to add a bulleted list to a word document that I'm generating automatically. From other answers I believe the code is correct, but the result doesn't produce any bullet points at all, it doesn't seem to apply the indent either. Any Ideas? Microsoft.Office.Interop.Word.Paragraph assets; assets = doc.Content.Paragraphs.Add(Type.Missing); // Some code to generate the text foreach (String asset in assetsList) { assetText = assetText + asset + "\n"; } assets.Range.ListFormat.ApplyBulletDefault(Type.Missing); // Add it to the document assets.Range