ive made a dll in c, and it gets called with visual basic.
in vb one of the parameters i pass is the address of a vb function.
i can get the dll to run the function fine, but i need to put that address into a struct to pass into a thread, which is where my problem is.

i have typedef'd the function pointer as

typedef void (*vbFuncEntry)(int);

a simplified version of the dll function header is

__declspec(dllexport) int __stdcall Function(LPTSTR msg, vbFuncEntry vbFE);

inside that function if i do something like

(vbFE)(500);

it calls the vb function no problem.

but when i try to assign that address into a struct, and call it later it doesnt work. im sure it just has to do with the fact that im assigning it wrong.

i have

typedef struct {
  char *msgPtr;
  vbFuncEntry funcEntry;
} info, *pinfo;

//and later on in the exported dll function

pinfo p2info = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(info));
p2info->funcEntry = vbFE;

//in the thread when i call
(p2info->funcEntry)(500);  //the program crashes

again i just think im doing that pointer assignment wrong, or it needs to be a pointer to a function pointer and i dont know how to do the assignment for it.

thanks for any help :)

Hmm, nwm (tried that with regular code and it worked fine)...

hey, thanks for the reply. i t turns out i dont think the assignment was my problem after all. just to make sure i ended up printing the memory address of the function pointer in different places in the code, and they all yielded the same address. i have a feeling the problem is that im trying to call it from inside a thread i crated. i didnt find anything on msdn createthread that would indicate that you cant call a function pointer in a thread, so i have a feeling it has to do more with vb than the c code. vb probably doesnt like creating a thread and then having it try to call another procedure inside the program. i have just decided to live with the application hanging for a second while it does its thing, instead of trying to thread it off in the dll and call vb when it finishes.
thanks for your help. if anyone has any explanations as to why it crashes in the thread calling a func pointer, thatd be cool too.

hey, thanks for the reply. i t turns out i dont think the assignment was my problem after all. just to make sure i ended up printing the memory address of the function pointer in different places in the code, and they all yielded the same address. i have a feeling the problem is that im trying to call it from inside a thread i crated. i didnt find anything on msdn createthread that would indicate that you cant call a function pointer in a thread, so i have a feeling it has to do more with vb than the c code. vb probably doesnt like creating a thread and then having it try to call another procedure inside the program. i have just decided to live with the application hanging for a second while it does its thing, instead of trying to thread it off in the dll and call vb when it finishes.
thanks for your help. if anyone has any explanations as to why it crashes in the thread calling a func pointer, thatd be cool too.

Hmm, I just thought of something...
My first thought was that vb and c may use different calling conventions, but since the regular code worked that seemed boggered...
But if the included header file defined the calling convention that could explain why it worked to to call it directly but not indirectly, however it should emit some warning on bad assignment though... But c beeing as it are usually be that you dont have warnings fully turned on or something.

And again that idea would also be buggered if you manage to do the indirect call without running it in a different thread (so you know you have isolated the exact cause of the problem, the next step would be to try to step though it in a debugger I guess)...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.