Solved

Manage VB6 callback problem with external C DLL

Posted on 2010-11-22
8
1,125 Views
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.
0
Comment
Question by:dvplayltd
  • 4
  • 3
8 Comments
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 250 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!  
0
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 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).
0
 
LVL 11

Expert Comment

by:MajorBigDeal
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.

http://stackoverflow.com/questions/4169028/how-to-write-a-callback-in-vb6

http://www.gamedev.net/community/forums/topic.asp?topic_id=322545

http://www.thevbzone.com/secrets.htm

http://support.microsoft.com/kb/181578
0
 

Author Comment

by:dvplayltd
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.

 Thanks.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:MajorBigDeal
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.  
0
 

Author Comment

by:dvplayltd
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.
0
 
LVL 11

Expert Comment

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

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now