Hi DaniWeb team,
I had to import in .NET a DLL with a function that takes as input some CString arguments.
So I developed a little wrapper dll for this function containing an unmanaged function that takes as input some const char*,"translates" them into CString and then passes them to the original Dll in such a way than from my managed app in .Net I could import my little wrapper (using Dllimport and MarshalAs) and pass to it some "string" object.
The build of all goes ok, but when I run my .net app using it crashes when calling the unmanaged dll (I saw it in debugging mode).
This is my dll code:
#include "stdafx.h"
#include "UpdateTrigger.h"
#define MAXMODULE 50
typedef int (__stdcall *cfunc)(unsigned int, CString, CString);
cfunc UpdateTriggerSlot;
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
//
extern "C" __declspec(dllexport) int fnUpdateTriggerBridge(unsigned int dsp, const char* triglistURL, const char * trigorderlistURL)
{
CString triglist(triglistURL);
CString trigorder(trigorderlistURL);
HINSTANCE hLib = LoadLibrary(_T("MSS_SDK.dll"));
if(hLib==NULL) {
return -1;
}
UpdateTriggerSlot =(cfunc)GetProcAddress((HINSTANCE)hLib, "DSP_UpdateTriggerSlot");
if(UpdateTriggerSlot==NULL) {
FreeLibrary((HINSTANCE)hLib);
return -1;
}
int result = UpdateTriggerSlot(dsp, triglist, trigorder);
FreeLibrary((HINSTANCE)hLib);
return result;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
where MSS_SDK.dll is the other unmanaged dll containing the function taking CString as input.
I import it in C # like that:
[DllImport("UpdateTrigger.dll", EntryPoint = "fnUpdateTriggerBridge", CallingConvention = CallingConvention.Cdecl)]
public static extern int _DSP_UpdateTriggerSlot(UInt32 dsphandle, [MarshalAs(UnmanagedType.LPStr)] string ListURL, [MarshalAs(UnmanagedType.LPStr)] string OrderListURL);
When I call this library from .NET the application crashes and an error pop-up appears, saying
Debug Error!
"Run-time check Failure #0: The file of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention"
What am I doing wrong?
Thanks in advance