I have a c# application, which calls a dll (which I made, so I can
modify the source code), passes there several parameters. The Dll
contains a thread pool. In the entry function which is called from c# a
new job is assigned to the thread pool. As soon as job is completed in
a separate thread, is calls back (from the thread) to the C#
application.
The problem is that sometimes (not at each time when the dll is called)
I'm getting a
Run-Time Check Failure #0 caused by different calling convention of a
pointer to the function an the function itself.
Here is the a hardly simpified source code of the DLL and of the C#
app:
*********[DLL src]************
typedef void (__stdcall *THREAD_CALLBACK)(char* s, int id);
//Entry function of the DLL
extern "C" __declspec(dllexport)
int _stdcall r(char* url_ptr, char* ip_ptr, int port, THREAD_CALLBACK
callback, int id)
{
/* Filling wp structure wp*/
.....................
/*and assiging a task to the thread pool*/
THREAD_POOL_TASK taskInfo = {id, worker, LPVOID(wp)};
m_Pool.AssignTask(&taskInfo);
return 0;
}
bool _cdecl worker(UINT taskId, LPVOID pData)
{
WORKER_PARAMS* wp = ((PWORKER_PARAMS)pData);
THREAD_CALLBACK callback = wp->callback;
......................................
/*Doing some job here and the calling back to the C# app:*/
callback(wbuf, id);
return true;
}
*********[C# src]************
[DllImport("winhttp_dll.dll", CallingConvention =
CallingConvention.StdCall,
CharSet = CharSet.Ansi)]
public static extern int r(StringBuilder url_ptr, StringBuilder
ip_ptr, int port, Callback cb, int id);
[UnmanagedFunctionPointer(CallingConvention.StdCall )]
public delegate void DelegatePopulateGrid(string s, int i);
public delegate void Callback(int address, int id);
public static Callback ArpCallback;
/* In some function we call */
ArpCallback = new LiveMonitor.Callback(this.RecieveResponse);
r(url, ip, port, ArpCallback, row_index);
/*In Recieve Response we do some job and return*/
So the problem is that the error occurs SOMETIMES. And not very often
since the amount of calls in significant. Also I have to say that in
important places semaphores are placed.
Thank you for your concideration and sorry my crappy english.
Anton.