dllimport

Marshalling string from c# to c++

家住魔仙堡 提交于 2019-12-04 05:52:10
问题 I am new in microsoft world. I have lot of problem trying to pass a simple string from c# to dll/c++ I have read a lot of post and documentation about but the problem is the same. C++ code extern "C" __declspec(dllexport) int Init( long l , char* url ); C# code [DllImport("MCRenderer.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = false)] public static extern int Init(long a, StringBuilder url); Init(hndl.ToInt64(), str ); what haeppen is that long value is passed correctly

__cdecl and __declspec calling conventions confusion

泪湿孤枕 提交于 2019-12-04 05:46:26
I am writing a DLL for a third party application. The main software engineer mentions that the application uses the __cdecl (/Gd) calling convention. That I need to make sure that I use that. Additionally, the third party has provided to me a C++ DLL skeleton which exports the functions as follows: #ifdef _EXPORTING #define DECLSPEC __declspec(dllexport) #else #define DECLSPEC __declspec(dllimport) #endif #ifdef __cplusplus extern "C" { #endif DECLSPEC int ICD_Create(char* id); .... .... I am kind of confused. Why the functions are being exported using __declspec convention instead of __cdedl?

Can different versions of DLL be loaded in same application?

回眸只為那壹抹淺笑 提交于 2019-12-04 05:23:10
My application uses one version of library (a.dll), I am using another DLL(b.dll) which in-turn uses older version of the same library (a.dll) that i use. I am building the application by embedding a manifest file. The DLL i use is also using a embedded manifest file. I am having both the versions of the library in my WinSXS folder. My application is not able to load the appropriate versions of DLLs. Will having a separate manifest file (not embedding into DLL) help resolving the problem? What is the work around? Your situation is exactly the situation WinSxS is supposed to solve. It should be

When is it appropriate to use the extern keyword without using the [DllImport] attribute?

情到浓时终转凉″ 提交于 2019-12-04 05:21:01
I was re-reading through some .Net documentation today when I noticed that the first portion of the extern keywords documentation claims: The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code. What caught my attention was that the document states that "a common use" of extern is that it is used with the DllImport attribute. This implies that there are other use-cases where DllImport is not required. I've not had to integrate many external

C# P\Invoke DLL no entry point into C++?

随声附和 提交于 2019-12-04 05:15:10
问题 I have a C++ Dll "TheFoo.dll" with a method "Foo()" I have access to other C++ code that uses this method by simply calling: Foo(); I believe the Method does have the: __declspec( dllexport ) So, with the reading I've done about P/Invoke, i thought i should be able to simply call the same method from my C# code: namespace PInvokeExample1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent

Free unmanaged memory allocation from managed code

孤街浪徒 提交于 2019-12-04 04:15:00
A .NET application calls C dll. The C code allocates memory for a char array and returns this array as result. The .NET applications gets this result as a string. The C code: extern "C" __declspec(dllexport) char* __cdecl Run() { char* result = (char*)malloc(100 * sizeof(char)); // fill the array with data return result; } The C# code: [DllImport("Unmanaged.dll")] private static extern string Run(); ... string result = Run(); // do something useful with the result and than leave it out of scope Some tests of it show that the garbage collector does not free the memory allocated by the C code.

How to import void * C API into C#?

随声附和 提交于 2019-12-04 03:06:23
Given this C API declaration how would it be imported to C#? int _stdcall z4ctyget(CITY_REC *, void *); I've been able to get this far: [DllImport(@"zip4_w32.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "z4ctygetSTD", ExactSpelling = false)] private extern static int z4ctygetSTD(ref CITY_REC args, void * ptr); Naturally in C# the "void *" doesn't compile. Some Googling indicates that it should be translated as "object." Which seems like it should work. But others indicate that "Void * is called a function pointer in C/C++ terms which in C# terms is a delegate". That doesn

C# delegate for C++ callback

陌路散爱 提交于 2019-12-04 01:46:16
I think I have basically understood how to write c# delegates for callbacks, but this one is confusing me. The c++ definition is as follows: typedef int (__stdcall* Callback)( long lCode, long lParamSize, void* pParam ); and my c# approach would be: unsafe delegate int CallbackDelegate (int lCode, int lParamSize, IntPtr pParam); Although this seems to be incorrect, because I get a PInvokeStackInbalance error, which means my definition of the delegate is wrong. The rest of the parameters of the function are strings or ints, which means they cannot cause the error, and if I just pass a IntPtr

A call to PInvoke function has unbalanced the stack when including a C DLL into C#

跟風遠走 提交于 2019-12-03 21:40:03
问题 I have written a C DLL and some C# code to test including this DLL and executing functions from it. I am not too familiar with this process, and am receiving a PInvokeStackImbalance exception whenever my DLL function is called from the C# source code. The code is as follows (I have commented most code out to isolate this problem): C# Inclusion code: using System; using System.Runtime.InteropServices; using System.IO; namespace TestConsoleGrids { class Program { [DllImport("LibNonthreaded.dll"

What's the equivalent of WORD in C#?

与世无争的帅哥 提交于 2019-12-03 11:01:46
I'm trying to access an unmanaged library and am lucky to have access to a comprehensive guide to the API. Unfortunately, I've no idea what the C# equivalent of C++'s WORD type is. Similarly, I've no idea what DWORD would be. yan You're probably looking for ushort for WORD and uint for DWORD. 来源: https://stackoverflow.com/questions/5490428/whats-the-equivalent-of-word-in-c