Solved

call C++ Dll

Posted on 2002-03-13
19
283 Views
Last Modified: 2008-02-01
Hi Experts,

I made a Dll in C++ and called it from VB:

in C++:
int main0(int argc, char *inpFileName, char *outFileName)

in VB:
Private Declare Function main0 Lib "test.dll" (ByVal argc As Long, ByVal inpFileName As String, ByVal outFileName As String) As Long
main0 3, App.Path & "\test.inp", App.Path & "\test.out"

when run the VB, got error:
Bad DLL calling convertion.

Can you tell me what I missed?
0
Comment
Question by:wellwet
[X]
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
  • 8
  • 3
  • 3
  • +3
19 Comments
 
LVL 6

Expert Comment

by:xSinbad
ID: 6863469
Try these;

Declare Sub main0 Lib "test.dll" ((ByVal argc As Long, ByVal inpFileName As String, ByVal
outFileName As String) As Long

call main0 (3, App.Path & "\test.inp", App.Path & "\test.out")

' OR this maybe

Private Declare Function main0 Lib "test.dll" (ByVal argc As Long, ByVal inpFileName As String, ByVal
outFileName As String) As Long


call main0 (3, App.Path & "\test.inp", App.Path & "\test.out")


Cheers
Marcus
0
 
LVL 6

Expert Comment

by:xSinbad
ID: 6863472
Woops extra ( in the first one.
0
 
LVL 5

Expert Comment

by:ianouii
ID: 6863559
Just open your VB project, go to reference, refer the VC++ dll.

use the normal

Dim objVC as YourDllName

from there, u can straight to use it.

objVC.classmember.

good luck. :>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wellwet
ID: 6864025
To xSinbad:

Thank you....but
>Declare Sub main0 Lib "test.dll" ((ByVal argc As Long, ByVal inpFileName As String, ByVal
outFileName As String) As Long
How a Sub returns a Long?

---------------------------------

To ianouii:

Thank you....but
>go to reference, refer the VC++ dll
Are you sure? What you said is for an ActiveX Dll...
0
 
LVL 6

Expert Comment

by:xSinbad
ID: 6864047
Woops.. remove the as long it is not required.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6864627
Did you try putting extern "C" in front of your function?

extern "C" int main0(int argc, char *inpFileName, char *outFileName)
0
 
LVL 3

Expert Comment

by:DennisL
ID: 6864686
Shouldn't the last 2 arguments be passed ByRef?

In C++ you're expecting a pointer to a string.

Private Declare Function main0 Lib "test.dll" (ByVal argc As Long, ByRef inpFileName As String, ByRef
outFileName As String) As Long
0
 

Author Comment

by:wellwet
ID: 6864687
Hi Triskelion,

>extern "C" int main0(int argc, char *inpFileName, char *outFileName)

Why we need that?
I used VC++ 6.0 to compile the source into DLL and it works for other simple examples....
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6864744
That's something to try.

Also try adding
   as integer
to the end of your declare so it is an int instead of a long.

Private Declare Function main0 Lib "test.dll" (ByVal argc As Long, ByVal inpFileName As String, ByVal
outFileName As String) As Integer


DennisL >> Shouldn't the last 2 arguments be passed ByRef?

No... ByVal
0
 

Author Comment

by:wellwet
ID: 6864791
>it is an int instead of a long

Well.... should be 'As Long' in VB for 'int' in C++ Dll....

>Shouldn't the last 2 arguments be passed ByRef?
Well...you have never call a C++ Dll with 'As String' :-(
0
 

Author Comment

by:wellwet
ID: 6864894
More information:

My code above works if I compile my VB stuff to an exe and then run it. I got the error when I run VB under the design window......
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6865108
I just made a fresh DLL and called it from Excel.
If you test your DLL with Excel, do you get the same results?
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 100 total points
ID: 6865671
You have to use the STDCALL calling convention if you are targeting VB. The DLL has to change.

0
 

Author Comment

by:wellwet
ID: 6865736
Hi DrDelphi,

In fact, I tried that before you post and I wes going to close the question:-)
What I used is _stdcall and was successfull.
Can you tell me
1. What is the stdcall for
2. What is the difference between _stdcall and STDCALL?
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6865834
_stdcall is correct. I only capitilized it for emphasis.

the calling convention has to do with how things get cleaned up and the order in which parameters are passed.

Good luck!!
0
 

Author Comment

by:wellwet
ID: 6865906
To Triskelion:

Thanks a lot for your comments...I am going to give the points to DrDelphi :-(
0
 

Author Comment

by:wellwet
ID: 6865932
To DrDelphi:

If we don't use _stdcall, thins will not cleaned and the parameters will not in order?

Why my original code (without _stdcall) works fine if I compile my VB stuff to an exe and then run it. I got the error only when I run my VB project under the design window......
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6866120
I can't say for sure, but I suspect that because in the IDE there is an invisible instance of your application running (so that VB can debug it realtime),the DLL has already done what it needed to do once for the "invisible" app, so that it blows up on the "real" app. Of course, I don't know this for sure. I  DO know that the calling convention is the problem, however, having been bitten a time or two myself by the same thing.


Good luck!!
 
0
 

Author Comment

by:wellwet
ID: 6866274
Thans a lot for the helps from all experts.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

726 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