Manage VB6 callback problem with external C DLL

Question:  Hi experts!

I have a Vb6 application which call C DLL via _stdcall. I manage to rule this external hardware no matter of some limits of VB6, with exception of callback.  Callback function do not works and I need to find a solution. Problem with callback function is that in Vb6 IDE it works fine, but in .exe file don’t work and make great crash (somethink even with blue screen  :-) )

 If I understand correct, problem is because the DLL calls these callback functions from other thread (for safety reason) and in this case VB6 can’t manage. More clearly, I get callback function OK even in exe file, but if I try to read a parameters of this callback it crash. I need these parameters!

 So I consider to create a application in VC C++ which cover these function and to use it in VB6. Question is – what type of application I need to create in C ++ ? Can it be OCX, in which thread are executed OCX ? Or it should be DLL? But this callback is best to be return like events, I think this can be done only with ActiveX.
Who is Participating?
I used to do VB6 a lot and had callbacks with DLL as you describe (although I;m not sure if I used stdcall).  My experience is that the code in the DLL always runs in the same thread as the code that calls it.  

It is true that using the IDE will mask threading problems. However, it is *NOT* because VB6 cannot do callbacks or multiple threads so changing to C++ by itself will probably not fix the issue.  I'm guessing that the problem is that there is something wrong with the format of the callback.  

I remember (again this was a long time ago), having similar problems.  The way I solved it was by careful step-by-step.  First, there is a way to see the thread id of the current thread. Putting that in your code allows you to determine exactly which thread is which.  Second, see if you can get the callback working without any parameters passing. Variables in VB6 are different internal format than in other languages. So this will tell you if the problem is with the parameters.

If I was still working in VB6 I would make you an example, but unfortunately I cannot.  I'll bet if you search experts-exchange, there are probably a lot of old threads that explain how to do this.  

I'm sorry I can't be more specific - best of luck to you!  
In addition to the above comment that you've got to make sure you're using the right type of variables (which can be VERY tricky if you are dealing with strings and don't know how VB handles strings and DLL function calles), you MUST use a function defined in a .BAS module.  I don't recall if the VB compile will complain if you attempt to take the AddressOf() a class or form function.

Can you post what the declaration for the C functions are (both the C DLL function you are calling, and what the Callback function declaration).
I agree with HooKooDooKu suggestion to post the declarations.  In addition to the declarations, the code that call sets up the callback and the code that actually callsback would be good. Presumably the first part of solving this problem will be getting any simple callback working.

Maybe figure out at what point the blowup happens. For example, when you try to set up the callback, does that code actually get control or does it fail before that?  If that works, then does the code that actually callsback get executed. And lastly, does the function being called back to ever get control.  That kind of thing.

Then second step is to get the parameter passing working, but one step at a time.    

Also, here are some links that might help. I suggest you try to create the simplest callback scenario that you possibly can and use the above step-by-step ideas to get that working first. If possible, it would be good to start with a new project just for development purposes. Then when you get it working you can transfer your new knowledge and techniques to your real project.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

dvplayltdAuthor Commented:
Thanks to all boys.

My callback function is in .BAS module. I make separated project for make this functionality, and on it I face with these problems.
Params which are with these callbacks are only numbers, not string.

Till now I detect that problem is somewhere in the system - level of Vb. This is 99 % sure. Problems is one of this: 1)VB6 doesn’t work fine with callback  2)function params are with type which is not present in Vb6, like _int64 . In some place it work with VB6 Currency type, but not in callback.

So my question is: does ActiveX written in C and with created instance from VB6 will have access to different thread or again will not? This is real question here, other is just to see all picture.

Sorry I'm having a little trouble understanding - if you call a DLL, the method called will be running on the same thread as the caller. I don't understand what "different thread" you are referring to.  
dvplayltdAuthor Commented:
DLL which I call use special technique to return callback in different thread that main which create him. This is Stradis decoder and he does this for safety reason.
Ahhh, I see. Do you know if the threads created qualify as single-thread apartments?
dvplayltdAuthor Commented:
10x. This question is reall hard ..look that I'll find answer when I make OCX in C ...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.