interop

Calling unmanaged function from C#: should I pass StringBuilder or use unsafe code?

人走茶凉 提交于 2019-12-05 08:04:09
I've got a C# program that needs to pass a char buffer to an unmanaged function. I've found two ways that seem to work reliably, but I'm not sure which I should choose. Here's the unmanaged function's signature. extern "C" __declspec(dllexport) int getNextResponse(char *buffer); The first option is to define the buffer as a StringBuilder, as follows. //at class level... [DllImport("mydll.dll")] static extern int getNextResponse(StringBuilder buffer); //in main method body... StringBuilder sb = new StringBuilder(" ", 65536); int rc = getNextResponse(sb); This is simple, and it works, and I

ASP.NET session and storing objects that use COM interop

泪湿孤枕 提交于 2019-12-05 07:41:26
问题 I'm working on an asp.net web site. We have to use com interop to interact with legacy vb6 activex components. The components in many cases rely on receiving a context object (which is itself a vb6 activex component) as a parameter. The context object is fairly costly to construct. Therefore one idea is that a context object is constructed once and stored in asp.net session. However, if this object is just a .net wrapper around an activex component, is it wise or advisible to persist such an

COMException in C# when hooking into event

拈花ヽ惹草 提交于 2019-12-05 07:32:51
I am receiving a COM Exception when trying to hook into an event on a COM Object. Here is the code I am trying to execute. COMClass a = IComClass as ComClass; a.SomeEvent += new SomeEvent_EventHandler(MethodNameHere); Line two throws an exception of type COMException with the following information: System.Runtime.InteropServices.COMException was caught Message="Exception from HRESULT: 0x80040202" Source="mscorlib" ErrorCode=-2147220990 StackTrace: at System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(Object pUnkSink, Int32& pdwCookie) Does anyone have any ideas why I am unable to

Replace bold text in MS Word 2007 with <b>text</b> using C#.NET

ぐ巨炮叔叔 提交于 2019-12-05 07:06:27
问题 I want to search all bold text occurrences in MS Word 2007 document, and replace each bold "text" with "< text >" Like following pseudo-code foreach boldText in WordDocument { string replacedText = "< " + boldText + " >"; WordDocument.replace(boldText ,replacedText ); } WordDocument.save(); 回答1: What you could do is something like this: private void ReplaceBoldText(Microsoft.Office.Interop.Word.Document doc) { foreach(Microsoft.Office.Interop.Word.Range rng in doc.StoryRanges) { foreach

CryptoAPI C++ interop with Java using AES

北城以北 提交于 2019-12-05 07:01:17
问题 I am trying to encrypt in C++ using CryptoAPI and decrypt Java using SunJCE. I have gotten the RSA key to work -- and verified on a test string. However, my AES key is not working -- I get javax.crypto.BadPaddingException: Given final block not properly padded . C++ Encryption: // init and gen key HCRYPTPROV provider; CryptAcquireContext(&provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT); // Use symmetric key encryption HCRYPTKEY sessionKey; DWORD exportKeyLen;

What is the proper PInvoke signature for a function that takes var args?

可紊 提交于 2019-12-05 06:55:38
There is a native function: int sqlite3_config(int, ...); I would like to PInvoke to this function. Currently, I have this declaration: [DllImport("sqlite3", EntryPoint = "sqlite3_config")] public static extern Result Config (ConfigOption option); (Result and ConfigOption are enums of the form enum Result : int { ... } .) I am actually only interested in the single parameter version of this function and don't need the other args. Is this correct? I am also curious as to how you would declare the two argument form (perhaps it would take 2 IntPtrs?). Shay Erlichmen You need to use the __arglist

Pinning Array of .NET objects

别说谁变了你拦得住时间么 提交于 2019-12-05 06:42:52
I would like to pin an array of .NET objects (including the objects) in order to allow a native function to do some processing on the objects. As far as I understood, GCHandle.Alloc() does not allow me to do this, because such an array contains references (and the objects might also contain references) which are not blittable. Is there any other option to achieve this? I would be okay with very hack-y suggestions or ones that require Mono. Arrays in .NET are represented in contiguous memory. So that means that in memory, after element 0, element 1 will come directly after the previous element,

Sync system time to domain controller using .NET code

£可爱£侵袭症+ 提交于 2019-12-05 06:29:58
I have time based tests to run that require changing the system time multiple times during the test. I want to be able to resync the time to the domain controller time at the end of the test. I there any way to do that using .NET code (C#). I am changing the time using the p-invoke function found in: Set time programmatically using C# Thanks One easy way would be to launch a process and run the NET TIME command (copied from http://blogs.msdn.com/sanket/archive/2006/11/02/synchronizing-machine-time-with-domain-controller.aspx ) using System; using System.Diagnostics; class Program { static void

C# Outlook 2007 COM interop application does not exit!

怎甘沉沦 提交于 2019-12-05 06:03:38
Any ideas why the following code does not exit the Outlook 2007 process created via COM interop? Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); var item = app.Session.OpenSharedItem("C:\\test.msg") as Microsoft.Office.Interop.Outlook.MailItem; string body = item.HTMLBody; int att = item.Attachments.Count; (item as Microsoft.Office.Interop.Outlook._MailItem).Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard); System.Runtime.InteropServices.Marshal.ReleaseComObject(item); (app as Microsoft.Office.Interop.Outlook._Application

raising a vb6 event using interop

岁酱吖の 提交于 2019-12-05 06:01:29
I have a legacy VB6 component that I've imported into VS using tlbimp.exe to generate my interop assembly. The VB6 component defines an event that allows me to pass messages within VB6. Public Event Message(ByVal iMsg As Variant, oCancel As Variant) I would really like to be able to raise this even in my C# program, but its getting imported as an event, not a delegate or something else useful. So, I can only listen, but never fire. Does anyone know how to fire an event contained within VB6? The C# event looks like [TypeLibType(16)] [ComVisible(false)] public interface __MyObj_Event { event _