How do you call a Visual C++ (DLL) function from Visual Basic 
Author Message
 How do you call a Visual C++ (DLL) function from Visual Basic

Need help with calling from within (Excel) VB a DLL function created within MSVC.

Even the simplest MSDN 'textbook' example does not seem to work, as follows:

I used the MSVC Development environment "Win32 Dynamic-Link Library" wizard (from File | New | Projects Tab) to create a sample DLL with sample variables and functions. I called it the Dll project MyDLL. The full declaration of the sample function that I want to call from within VB is:

MYDLL_API int fnMyDll(void)

{

return 770;

Quote:
}

where MYDLL_API is declared in the .h file as:

#define MYDLL_API __declspec(dllexport)

I want to call the int fnMyDll(void) function from within an Excel Visual Basic program.

To find out how to "declare" a "link" to a DLL from a Visual Basic program, I copied the style of the MSDN (CD ROM Version 6) help page entitled "Using a DLL Procedure in Your Application", and wrote a simple VB test bed as follows:

Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As Long

'Excel Test Bed For Dll

Sub TestBed()

Dim FnRetVal As Long

FnRetVal = fnMyDll()

End Sub

When stepping through the above VB code piece, it seems to recognize the dll MyDll and the function fnMyDll( ), however FnRetVal does not receive the return value 770.

I also tried running the code piece within regular VB (not VBA for any Office app) but this time not only does it not work but I get the following error msg:

'Run-time error '453':

Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'

Clicking on Help causes the follwoing MSDN page to appear:

Specified DLL function not found (Error 453)

The dynamic-link library (DLL) in a user library reference was found, but the DLL function specified wasn't found within the DLL. This error has the following causes and solutions:

You specified an invalid ordinal in the function declaration.

Check for the proper ordinal or call the function by name.

You gave the right DLL name, but it isn't the version that contains the specified function.

You may have the correct version on your machine, but if the directory containing the wrong version precedes the directory containing the correct one in your path, the wrong DLL is accessed. Check your machine for different versions. If you have an early version, contact the supplier for a later version.

If you are working on a 32-bit Microsoft Windows platform, both the DLL name and alias (if used) must be correct.

Make sure the DLL name and alias are correct.

Some 32-bit DLLs contain functions with slightly different versions to accommodate both Unicode and ANSI strings. An "A" at the end of the function name specifies the ANSI version. A "W" at the end of the function name specifies the Unicode version.

If the function takes string-type arguments, try appending an "A" to the function name.

For additional information, select the item in question and press F1.

None of the points mentioned seem to be the problem in my case.

Anybody out there who knows enough (VC and) VB programming to be able to tell me what's wrong?

Avraham.



Tue, 12 Aug 2003 03:06:55 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic
[Please post plain text, not HTML - this is not the web, and many
 people do not use HTML-capable newsreaders.]

Quote:

> Need help with calling from within (Excel) VB a DLL function created
> within MSVC.

> I used the MSVC Development environment "Win32 Dynamic-Link Library"
> wizard (from File | New | Projects Tab) to create a sample DLL with
> sample variables and functions. I called it the Dll project MyDLL.
> The full declaration of the sample function that I want to call from
> within VB is:

> MYDLL_API int fnMyDll(void)

> {

> return 770;

> }

> where MYDLL_API is declared in the .h file as:

> #define MYDLL_API __declspec(dllexport)

Two problems:
a) The calling convention is not __stdcall, the method used for Windows
   API functions and the only option accepted by VB

b) The export will be decorated, either as _fnMyDll for __cdecl,

   linkage (to support overloading).  To avoid this, you need to put
   an entry in the DEF file (that I believe the wizard created) for
   the export, like so:


   in the exports section.

Adding 'extern "C"' in front might not be a bad idea either.  The end
result looks something like this:

extern "C" MYDLL_API __stdcall int fnMyDll(void)
{
    return 770;

Quote:
}
> I want to call the int fnMyDll(void) function from within an Excel Visual Basic program.

[...]

Quote:
> Private Declare Function fnMyDll _
> Lib "D:\MyDll\Debug\MyDll.Dll" () As Long
> Sub TestBed()
>     Dim FnRetVal As Long
>     FnRetVal = fnMyDll()
> End Sub
> When stepping through the above VB code piece, it seems to recognize
> the dll MyDll and the function fnMyDll( ), however FnRetVal does not
> receive the return value 770.

That's interesting.  I would have expected an error.  I'd guess that if
you put in error trapping, you might get one.

Quote:
> I also tried running the code piece within regular VB (not VBA for
> any Office app) but this time not only does it not work but I get
> the following error msg:

> 'Run-time error '453':

> Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'

That means that the name of the function was decorated in MyDll.dll,
presumably either __cdecl or C++ name mangling.  Even if you had
exported it under the expected name, you would still have gotten
an error message due to the bad calling convention (don't recall what
the number is).


Tue, 12 Aug 2003 07:00:06 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic
You need to use the StdCall parameter passing method.
Quote:

>This is a multi-part message in MIME format.

>------=_NextPart_000_0033_01C09D13.62E6B480
>Content-Type: text/plain;
>    charset="windows-1255"
>Content-Transfer-Encoding: quoted-printable

>Need help with calling from within (Excel) VB a DLL function created =
>within MSVC.

>Even the simplest MSDN 'textbook' example does not seem to work, as =
>follows:

>I used the MSVC Development environment "Win32 Dynamic-Link Library" =
>wizard (from File | New | Projects Tab) to create a sample DLL with =
>sample variables and functions. I called it the Dll project MyDLL. The =
>full declaration of the sample function that I want to call from within =
>VB is:

>MYDLL_API int fnMyDll(void)

>{

>return 770;

>}

>where MYDLL_API is declared in the .h file as:

>#define MYDLL_API __declspec(dllexport)

>I want to call the int fnMyDll(void) function from within an Excel =
>Visual Basic program.

>To find out how to "declare" a "link" to a DLL from a Visual Basic =
>program, I copied the style of the MSDN (CD ROM Version 6) help page =
>entitled "Using a DLL Procedure in Your Application", and wrote a simple =
>VB test bed as follows:

>Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As =
>Long

>'Excel Test Bed For Dll

>Sub TestBed()

>Dim FnRetVal As Long

>FnRetVal =3D fnMyDll()

>End Sub

>When stepping through the above VB code piece, it seems to recognize the =
>dll MyDll and the function fnMyDll( ), however FnRetVal does not receive =
>the return value 770.=20

>I also tried running the code piece within regular VB (not VBA for any =
>Office app) but this time not only does it not work but I get the =
>following error msg:

>'Run-time error '453':

>Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'

>Clicking on Help causes the follwoing MSDN page to appear:

>Specified DLL function not found (Error 453)

>The dynamic-link library (DLL) in a user library reference was found, =
>but the DLL function specified wasn't found within the DLL. This error =
>has the following causes and solutions:=20

>You specified an invalid ordinal in the function declaration.=20

>Check for the proper ordinal or call the function by name.

>You gave the right DLL name, but it isn't the version that contains the =
>specified function.=20

>You may have the correct version on your machine, but if the directory =
>containing the wrong version precedes the directory containing the =
>correct one in your path, the wrong DLL is accessed. Check your machine =
>for different versions. If you have an early version, contact the =
>supplier for a later version.

>If you are working on a 32-bit Microsoft Windows platform, both the DLL =
>name and alias (if used) must be correct.=20

>Make sure the DLL name and alias are correct.

>Some 32-bit DLLs contain functions with slightly different versions to =
>accommodate both Unicode and ANSI strings. An "A" at the end of the =
>function name specifies the ANSI version. A "W" at the end of the =
>function name specifies the Unicode version.=20

>If the function takes string-type arguments, try appending an "A" to the =
>function name.

>For additional information, select the item in question and press F1.

>None of the points mentioned seem to be the problem in my case.

>Anybody out there who knows enough (VC and) VB programming to be able to =
>tell me what's wrong?

>Avraham.

>------=_NextPart_000_0033_01C09D13.62E6B480
>Content-Type: text/html;
>    charset="windows-1255"
>Content-Transfer-Encoding: quoted-printable

><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
><HTML><HEAD>
><META content=3D"text/html; charset=3Dwindows-1255" =
>http-equiv=3DContent-Type>
><META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
><STYLE></STYLE>
></HEAD>
><BODY>
><DIV><FONT face=3DArial size=3D2>
><DIV><B><U><FONT size=3D2>
><P>Need help with calling from within (Excel) VB a DLL function created =
>within=20
>MSVC.</P></B></U></FONT><FONT face=3DArial size=3D2></FONT><FONT =
>size=3D2>
><P>Even the simplest MSDN 'textbook' example does not seem to work, as=20
>follows:</P>
><P>I used the MSVC Development environment "Win32 Dynamic-Link Library" =
>wizard=20
>(from <B><U>F</B></U>ile | <B><U>N</B></U>ew | Projects Tab) to create a =
>sample=20
>DLL with sample variables and functions. I called it the Dll project =
>MyDLL. The=20
>full declaration of the sample function that I want to call from within =
>VB=20
>is:</P></FONT><FONT face=3D"Times New Roman" size=3D2></FONT><B><FONT=20
>face=3D"Courier New" size=3D1>
><P>MYDLL_API int fnMyDll(void)</P>
><P>{</P>
><P>return 770;</P>
><P>}</P></B></FONT><FONT face=3D"Times New Roman" size=3D2></FONT><FONT =
>size=3D2>
><P>where MYDLL_API is declared in the .h file as:</P></FONT><FONT=20
>face=3D"Times New Roman" size=3D2></FONT><B><FONT face=3D"Courier New" =
>size=3D1>
><P>#define MYDLL_API __declspec(dllexport)</P></B></FONT><FONT=20
>face=3D"Times New Roman" size=3D2></FONT><FONT size=3D2>
><P>I want to call the <B>int fnMyDll(void)</B> function from within an =
>Excel=20
>Visual Basic program.</P>
><P>To find out how to "declare" a "link" to a DLL from a Visual Basic =
>program, I=20
>copied the style of the MSDN (CD ROM Version 6) help page entitled =
>"<B>Using a=20
>DLL Procedure in Your Application</B>", and wrote a simple VB test bed =
>as=20
>follows:</P></FONT><FONT face=3D"Times New Roman" =
>size=3D2></FONT><B><FONT=20
>face=3D"Courier New" size=3D1>
><P>Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As =

>Long</P></FONT><FONT color=3D#008000 face=3D"Courier New" size=3D1>
><P>'Excel Test Bed For Dll</P></FONT><FONT face=3D"Courier New" =
>size=3D1>
><P>Sub TestBed()</P>
><P>Dim FnRetVal As Long</P>
><P>FnRetVal =3D fnMyDll()</P>
><P>End Sub</P></B></FONT><FONT face=3D"Times New Roman" =
>size=3D2></FONT><FONT=20
>size=3D2>
><P>When stepping through the above VB code piece, it seems to recognize =
>the dll=20
><B>MyDll</B> and the function <B>fnMyDll( )</B>, however <B>FnRetVal =
></B>does=20
>not receive the return value 770. </P>
><P>I also tried running the code piece within regular VB (not VBA for =
>any Office=20
>app) but this time not only does it not work but I get the following =
>error=20
>msg:</P><B>
><P>'Run-time error '453':</P>
><P>Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( =
>)'</P></B>
><P>Clicking on Help causes the follwoing MSDN page to =
>appear:</P></FONT><FONT=20
>face=3D"Times New Roman" size=3D2></FONT><B><FONT size=3D1>
><P>Specified DLL function not found (Error 453)</P></B>
><P>The </FONT><U><FONT color=3D#0000ff size=3D1>dynamic-link library=20
>(DLL)</U></FONT><FONT size=3D1> in a user library reference was found, =
>but the DLL=20
>function specified wasn't found within the DLL. This error has the =
>following=20
>causes and solutions: </P>
><DIR>
><DIR>
><P>You specified an invalid ordinal in the function declaration. </P>
><P>Check for the proper ordinal or call the function by name.</P>
><P>You gave the right DLL name, but it isn't the version that contains =
>the=20
>specified function. </P>
><P>You may have the correct version on your machine, but if the =
>directory=20
>containing the wrong version precedes the directory containing the =
>correct one=20
>in your path, the wrong DLL is accessed. Check your machine for =
>different=20
>versions. If you have an early version, contact the supplier for a later =

>version.</P>
><P>If you are working on a 32-bit Microsoft Windows platform, both the =
>DLL name=20
>and alias (if used) must be correct. </P>
><P>Make sure the DLL name and alias are correct.</P>
><P>Some 32-bit DLLs contain functions with slightly different versions =
>to=20
>accommodate both </FONT><U><FONT color=3D#0000ff =
>size=3D1>Unicode</U></FONT><FONT=20
>size=3D1> and </FONT><U><FONT color=3D#0000ff =
>size=3D1>ANSI</U></FONT><FONT size=3D1>=20
>strings. An "A" at the end of the function name specifies the ANSI =
>version. A=20
>"W" at the end of the function name specifies the Unicode version. </P>
><P>If the function takes string-type arguments, try appending an "A" to =
>the=20
>function name.</P></DIR></DIR>
><P>For additional information, select the item in question and press=20
>F1.</P></FONT><FONT face=3D"Times New Roman" size=3D2></FONT><FONT =
>size=3D2>
><P>None of the points mentioned seem to be the problem in my case.</P>
><P>Anybody out there who knows enough (VC and) VB programming to be able =
>to tell=20
>me what's wrong?</P>
><P></FONT></FONT><FONT face=3DArial=20
>size=3D2>Avraham.</P></DIV></DIV></FONT></BODY></HTML>

>------=_NextPart_000_0033_01C09D13.62E6B480--



Tue, 12 Aug 2003 18:19:13 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic
you have to add a .def file to your C++-Project and write the following lines:

EXPORTS
    fnMyDll

and your C++-declaration should look like

extern "C" int WINAPI fnMyDll(void)
{
    return 770;

Quote:
}

that's the way i always use for my dll's...
Florian


Tue, 12 Aug 2003 21:14:24 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic

Quote:
> Two problems:
> a) The calling convention is not __stdcall, the method used for Windows
>    API functions and the only option accepted by VB

False. __declspec( dllexport ) works just fine with VB. I use it all the
time in my DLLs.

Steve



Wed, 13 Aug 2003 02:07:47 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic

Reports on answers to my --- Question: How do you call a Visual C++ (DLL) function from Visual Basic


Quote:
>> you have to add a .def file to your C++-Project and write the following lines:

EXPORTS
    fnMyDll

and your C++-declaration should look like

extern "C" int WINAPI fnMyDll(void)
{
    return 770;

Quote:
}

That method works!
-----------------------------------------------------------------------


'    EXPORTS
        fnMyDll '

All the articles in my MSDN CD help (Ver 6.0) under the title "Export from a DLL " say that if you use "__declspec( dllexport )" (which was the original method that I tried), then you don't need to use a DEF file (with regards to exporting). For some reason out of all the things I did to get the DLL working, going against the documentation was not something that I tried, even though I had suspected that maybe that was the problem. Wasted hours!
-----------------------------------------------------------------------

* To the guy who suggested as follows:

Quote:
>> Read this article in the MSDN....Q106553

The article is an old one. It suggests declaring DLL functions as "__export CALLBACK " and provides an enormous DEF file. It did not try it.
-----------------------------------------------------------------------


Quote:
>> You need to use the StdCall parameter passing method.

That method did not work for me, for some reason.
-----------------------------------------------------------------------

* CONCLUSION

Now that I see that "__declspec( dllexport )" does in fact work if you supply a def file, then I will stick to that since it is basically the solution autogenerated by the MSVC DLL wizard. (If I had wanted the MFC version for DLLs then it would have autogenned also the DEF file and I would never have run into the problem).

Thanks everybody.

Avraham.


  Need help with calling from within (Excel) VB a DLL function created within MSVC.

  Even the simplest MSDN 'textbook' example does not seem to work, as follows:

  I used the MSVC Development environment "Win32 Dynamic-Link Library" wizard (from File | New | Projects Tab) to create a sample DLL with sample variables and functions. I called it the Dll project MyDLL. The full declaration of the sample function that I want to call from within VB is:

  MYDLL_API int fnMyDll(void)

  {

  return 770;

  }

  where MYDLL_API is declared in the .h file as:

  #define MYDLL_API __declspec(dllexport)

  I want to call the int fnMyDll(void) function from within an Excel Visual Basic program.

  To find out how to "declare" a "link" to a DLL from a Visual Basic program, I copied the style of the MSDN (CD ROM Version 6) help page entitled "Using a DLL Procedure in Your Application", and wrote a simple VB test bed as follows:

  Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As Long

  'Excel Test Bed For Dll

  Sub TestBed()

  Dim FnRetVal As Long

  FnRetVal = fnMyDll()

  End Sub

  When stepping through the above VB code piece, it seems to recognize the dll MyDll and the function fnMyDll( ), however FnRetVal does not receive the return value 770.

  I also tried running the code piece within regular VB (not VBA for any Office app) but this time not only does it not work but I get the following error msg:

  'Run-time error '453':

  Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'

  Clicking on Help causes the follwoing MSDN page to appear:

  Specified DLL function not found (Error 453)

  The dynamic-link library (DLL) in a user library reference was found, but the DLL function specified wasn't found within the DLL. This error has the following causes and solutions:

  You specified an invalid ordinal in the function declaration.

  Check for the proper ordinal or call the function by name.

  You gave the right DLL name, but it isn't the version that contains the specified function.

  You may have the correct version on your machine, but if the directory containing the wrong version precedes the directory containing the correct one in your path, the wrong DLL is accessed. Check your machine for different versions. If you have an early version, contact the supplier for a later version.

  If you are working on a 32-bit Microsoft Windows platform, both the DLL name and alias (if used) must be correct.

  Make sure the DLL name and alias are correct.

  Some 32-bit DLLs contain functions with slightly different versions to accommodate both Unicode and ANSI strings. An "A" at the end of the function name specifies the ANSI version. A "W" at the end of the function name specifies the Unicode version.

  If the function takes string-type arguments, try appending an "A" to the function name.

  For additional information, select the item in question and press F1.

  None of the points mentioned seem to be the problem in my case.

  Anybody out there who knows enough (VC and) VB programming to be able to tell me what's wrong?

  Avraham.

Shevach

  Need help with calling from within (Excel) VB a DLL function created within MSVC.

  Even the simplest MSDN 'textbook' example does not seem to work, as follows:

  I used the MSVC Development environment "Win32 Dynamic-Link Library" wizard (from File | New | Projects Tab) to create a sample DLL with sample variables and functions. I called it the Dll project MyDLL. The full declaration of the sample function that I want to call from within VB is:

  MYDLL_API int fnMyDll(void)

  {

  return 770;

  }

  where MYDLL_API is declared in the .h file as:

  #define MYDLL_API __declspec(dllexport)

  I want to call the int fnMyDll(void) function from within an Excel Visual Basic program.

  To find out how to "declare" a "link" to a DLL from a Visual Basic program, I copied the style of the MSDN (CD ROM Version 6) help page entitled "Using a DLL Procedure in Your Application", and wrote a simple VB test bed as follows:

  Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As Long

  'Excel Test Bed For Dll

  Sub TestBed()

  Dim FnRetVal As Long

  FnRetVal = fnMyDll()

  End Sub

  When stepping through the above VB code piece, it seems to recognize the dll MyDll and the function fnMyDll( ), however FnRetVal does not receive the return value 770.

  I also tried running the code piece within regular VB (not VBA for any Office app) but this time not only does it not work but I get the following error msg:

  'Run-time error '453':

  Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'

  Clicking on Help causes the follwoing MSDN page to appear:

  Specified DLL function not found (Error 453)

  The dynamic-link library (DLL) in a user library reference was found, but the DLL function specified wasn't found within the DLL. This error has the following causes and solutions:

  You specified an invalid ordinal in the function declaration.

  Check for the proper ordinal or call the function by name.

  You gave the right DLL name, but it isn't the version that contains the specified function.

  You may have the correct version on your machine, but if the directory containing the wrong version precedes the directory containing the correct one in your path, the wrong DLL is accessed. Check your machine for different versions. If you have an early version, contact the supplier for a later version.

  If you are working on a 32-bit Microsoft Windows platform, both the DLL name and alias (if used) must be correct.

  Make sure the DLL name and alias are correct.

  Some 32-bit DLLs contain functions with slightly different versions to accommodate both Unicode and ANSI strings. An "A" at the end of the function name specifies the ANSI version. A "W" at the end of the function name specifies the Unicode version.

  If the function takes string-type arguments, try appending an "A" to the function name.

  For additional information, select the item in question and press F1.

  None of the points mentioned seem to be the problem in my case.

  Anybody out there who knows enough (VC and) VB programming to be able to tell me what's wrong?

  Avraham.



Fri, 15 Aug 2003 00:53:57 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic

Quote:

> you have to add a .def file to your C++-Project and write the following lines:

> EXPORTS
>     fnMyDll

> and your C++-declaration should look like

> extern "C" int WINAPI fnMyDll(void)
> {
>     return 770;
> }

I believe in the absence of a __stdcall specification, this will
produce a bad calling convention error... though in this particular
case (with no parameters to be misordered) you might get away with it.


Fri, 15 Aug 2003 23:03:26 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic

Quote:

> > Two problems:
> > a) The calling convention is not __stdcall, the method used for Windows
> >    API functions and the only option accepted by VB

> False. __declspec( dllexport ) works just fine with VB. I use it all the
> time in my DLLs.

__stdcall and __declspec( dllexport ) are not, AFAIK, mutually
exclusive.

You should note that I never suggested that the OP remove
__declspec( dllexport ) from his/her code (it was originally present
in the form of MYDLL_API, and I left that in my suggested declaration).



Fri, 15 Aug 2003 23:01:37 GMT  
 How do you call a Visual C++ (DLL) function from Visual Basic
wrong. I always use it for my dll calls in vb, with lots of parameters, it works just fine.
I guess the __stdcall is defined in the WINAPI.

Florian


Quote:

> > you have to add a .def file to your C++-Project and write the following lines:

> > EXPORTS
> >     fnMyDll

> > and your C++-declaration should look like

> > extern "C" int WINAPI fnMyDll(void)
> > {
> >     return 770;
> > }

> I believe in the absence of a __stdcall specification, this will
> produce a bad calling convention error... though in this particular
> case (with no parameters to be misordered) you might get away with it.



Sat, 16 Aug 2003 01:23:16 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0

2. How to write Visual C++ DLL's and call them from Visual Basic

3. How to call c++ functions from visual basic

4. Visual Basic - C++ Function Calling <--- HELP !

5. Export function from Visual C++ to be used in Visual Basic

6. Calling a VB function/procedure from Visual C++ DLL

7. adapting Visual C++ dll for Visual Basic

8. Calling a Visual Basic DLL from C++

9. More on calling Vis C++ DLLs from Visual Basic

10. Calling a C++ Dll in Visual Basic.

11. Solution: Calling C++ dll from Visual Basic

12. Calling C++ DLLs from Visual Basic

 

 
Powered by phpBB® Forum Software