By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,163 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,163 IT Pros & Developers. It's quick & easy.

Problem with unmanaged DLL callbacks

P: n/a
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#

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#

*********[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)};

return 0;

bool _cdecl worker(UINT taskId, LPVOID 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 =
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.

Jul 27 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.