[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Manage VB6 callback problem with external C DLL

Posted on 2010-11-22
Medium Priority
Last Modified: 2012-06-21

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.
Question by:dvplayltd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 11

Accepted Solution

MajorBigDeal earned 1000 total points
ID: 34192947
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!  
LVL 16

Assisted Solution

HooKooDooKu earned 1000 total points
ID: 34193495
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).
LVL 11

Expert Comment

ID: 34193828
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.




What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 34197816
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.

LVL 11

Expert Comment

ID: 34200280
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.  

Author Comment

ID: 34200346
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.
LVL 11

Expert Comment

ID: 34200789
Ahhh, I see. Do you know if the threads created qualify as single-thread apartments?

Author Closing Comment

ID: 34215955
10x. This question is reall hard ..look that I'll find answer when I make OCX in C ...

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question