none
creating a dll in Visual C++ 2010 Express for use in Visual Basic 2008

    Question

  • Hi all,

    I am a novice programmer, and I am trying to learn how to create a dll in Visual C++ that  I can use in Visual Basic.  I am using the Visual C++ 2010 express edition IDE and the Visual Basic 2008 Express edition IDE.

    I found some tutorials online, but the ones that I found had examples for earlier editions of VB, which looks different to me.  Nonetheless, I tried following one of these tutorials, making changes to the VB code where I noticed differences.

    The tutorial that I tried following is at http://logix4u.net/Programming/vc++/A_Tutorial_on_creating_DLLs_with_VC++.html.  I worked with the first example, which is writing a C function to sum two integers.

    I was able to create a dll in VC++, but I found that I could not register the dll - when I tried using RegSvr32 (first option when right clicking on dll), I receive the error "(my dll name) was loaded, but the DllRegisterServer entry point was not found.  This file can not be registered."

    I went ahead and tried using this dll in my VB application.  When I run the VB application, I get no result - when I click the button that is meant to sum the values of the two text box inputs, I get no result.  Notably, I receive no error messages.

    Today, I was looking at using dumpbin.exe to "probe" the dll that I created.  I looked at the exports, and here is what the dumpbin output shows me:

    Dump of file C:\WINDOWS\system32\example1.dll

    File Type: DLL

      Section contains the following exports for example1.dll

        00000000 characteristics
        4D97B5D9 time date stamp Sat Apr 02 19:48:41 2011
            0.00 version
               1 ordinal base
               1 number of functions
               1 number of names

        ordinal hint RVA      name

              1    0 00011046 sum = @ILT+65(?sum@@YGHHH@Z)

      Summary

            1000 .data
            1000 .idata
            2000 .rdata
            1000 .reloc
            1000 .rsrc
            4000 .text
           10000 .textbss

    From what I have read on the output of dumpbin, ?sum@@YGHHH@Z looks to me like a "mangled name" for my exported function.  But the tutorial that I was using for using dumpbin ( http://msdn.microsoft.com/en-us/library/aa446532.aspx) seems to be saying that by using a definition file, the un-mangled name should be shown (although I may be misunderstanding this...).  When I created my dll, I used the following def file:

    LIBRARY example1

    EXPORTS

    sum    @1

    To include the def file, I did the following:

    Under solution explorer, I right-clicked on my project name. Then I expanded the options for linker, and under the "input" option, I placed the def file by path and name in the box labeled "Module Definition File." 

    My questions are:

    (1) should I be able to register my dll with RegSvr32?

    (2) Did I include the def file in the correct location under linker options?

    (3) I did not put the path and file name for the def file in quotation marks - should I have done that?

    (4) Do you see any other place I may be making an error in the dll creation?

    Thanks for any help!  Also, please let me know if I need to provide any other details to help in getting the question answered.

    Jason

     

     

    Sunday, April 03, 2011 3:43 PM

Answers

  • I am a novice programmer, and I am trying to learn how to create a dll in Visual C++ that  I can use in Visual Basic.  I am using the Visual C++ 2010 express edition IDE and the Visual Basic 2008 Express edition IDE.

    Not sure why you're mixing 2 different releases of the products, but
    never mind...

    The normal case for using a C++ DLL is to make use of an existing body
    of code that you want to retain as C/C++. In this situation I've found
    the neatest route is to create a C++/CL wrapper DLL that provides an
    interface that you can easily use from a .Net application.

    What I'd do is design your interface as though you were calling it in
    the same language (VB.Net in your case) and then do the equivalent in
    C++/CLI.

    From the C++/CLI code you can fairly seamlessly call native C/C++ code
    as though it was all a native C/C++ application.

    Having a C++/CLI DLL will enable your to add a reference to that
    component in your VB.Net project in the same way that you normally do
    for other .Net components.

    I was able to create a dll in VC++, but I found that I could not register the dll - when I tried using RegSvr32 (first option when right clicking on dll), I receive the error "(my dll name) was loaded, but the DllRegisterServer entry point was not found.  This file can not be registered."

    You only need to register COM type DLLs, so that's not surprising.

    I went ahead and tried using this dll in my VB application.  When I run the VB application, I get no result - when I click the button that is meant to sum the values of the two text box inputs, I get no result.  Notably, I receive no error messages.

    Unless I've missed something. you've not said how you're calling your
    DLL code from VB.

    From what I have read on the output of dumpbin, ?sum@@YGHHH@Z looks to me like a "mangled name" for my exported function. 

    Correct. The undname utility will help you here...

    undname ?sum@@YGHHH@Z
    Microsoft (R) C++ Name Undecorator
    Copyright (C) Microsoft Corporation. All rights reserved.

    Undecoration of :- "?sum@@YGHHH@Z"
    is :- "int __stdcall sum(int,int)"

    (1) should I be able to register my dll with RegSvr32?

    No - unless it's a COM DLL or one that has the appropriate exports for
    some other reason.

    (2) Did I include the def file in the correct location under linker options?

    Yes, I think so.

    (3) I did not put the path and file name for the def file in quotation marks - should I have done that?

    Not as far as I'm aware. If something like that were wrong I'd expect
    some weird error during the build process.

    (4) Do you see any other place I may be making an error in the dll creation?

    No.

    Dave

    Sunday, April 03, 2011 4:42 PM

All replies

  • I am a novice programmer, and I am trying to learn how to create a dll in Visual C++ that  I can use in Visual Basic.  I am using the Visual C++ 2010 express edition IDE and the Visual Basic 2008 Express edition IDE.

    Not sure why you're mixing 2 different releases of the products, but
    never mind...

    The normal case for using a C++ DLL is to make use of an existing body
    of code that you want to retain as C/C++. In this situation I've found
    the neatest route is to create a C++/CL wrapper DLL that provides an
    interface that you can easily use from a .Net application.

    What I'd do is design your interface as though you were calling it in
    the same language (VB.Net in your case) and then do the equivalent in
    C++/CLI.

    From the C++/CLI code you can fairly seamlessly call native C/C++ code
    as though it was all a native C/C++ application.

    Having a C++/CLI DLL will enable your to add a reference to that
    component in your VB.Net project in the same way that you normally do
    for other .Net components.

    I was able to create a dll in VC++, but I found that I could not register the dll - when I tried using RegSvr32 (first option when right clicking on dll), I receive the error "(my dll name) was loaded, but the DllRegisterServer entry point was not found.  This file can not be registered."

    You only need to register COM type DLLs, so that's not surprising.

    I went ahead and tried using this dll in my VB application.  When I run the VB application, I get no result - when I click the button that is meant to sum the values of the two text box inputs, I get no result.  Notably, I receive no error messages.

    Unless I've missed something. you've not said how you're calling your
    DLL code from VB.

    From what I have read on the output of dumpbin, ?sum@@YGHHH@Z looks to me like a "mangled name" for my exported function. 

    Correct. The undname utility will help you here...

    undname ?sum@@YGHHH@Z
    Microsoft (R) C++ Name Undecorator
    Copyright (C) Microsoft Corporation. All rights reserved.

    Undecoration of :- "?sum@@YGHHH@Z"
    is :- "int __stdcall sum(int,int)"

    (1) should I be able to register my dll with RegSvr32?

    No - unless it's a COM DLL or one that has the appropriate exports for
    some other reason.

    (2) Did I include the def file in the correct location under linker options?

    Yes, I think so.

    (3) I did not put the path and file name for the def file in quotation marks - should I have done that?

    Not as far as I'm aware. If something like that were wrong I'd expect
    some weird error during the build process.

    (4) Do you see any other place I may be making an error in the dll creation?

    No.

    Dave

    Sunday, April 03, 2011 4:42 PM
  • Hi Dave,

     

    Thank you for your help.  You're right, I did not specify how I called the DLL code in VB.  Here is the code from VB:

    Private Declare Function sum Lib "C:\WINDOWS\system32\example1.dll" (ByVal x As Long, ByVal y As Long) As Long

    Thanks for pointing me to the undname utility.

     

    Jason

    Sunday, April 03, 2011 6:24 PM
  • Thank you for your help.  You're right, I did not specify how I called the DLL code in VB.  Here is the code from VB:

    Private Declare Function sum Lib "C:\WINDOWS\system32\example1.dll" (ByVal x As Long, ByVal y As Long) As Long

    OK, you said when you ran the code you had no result - what do you
    mean specifically?

    Dave

    Sunday, April 03, 2011 6:33 PM
  • In the form, I put three textboxes and a button.  The first two are where the values to be added are entered, and the third is to show the result that is computed when the button is clicked.

    What I meant when I said that I was getting no result was this: when I entered values in the two input text boxes and clicked the button, no result was displayed - the third text box remained empty.

    I figured out the problem here - I had Private Sub Button 1 Click () instead of

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Once I changed this, I started getting a result in text box 3, although it is the incorrect result.  The result that I obtain is equal to the value in text box 1, instead of the sum of text box 1 and text box 2.

    I will keep working on this myself and come back later if I have any specific questions - I think this is heading outside the scope of my original question. 

    Thanks again for your help.

    Jason

    Sunday, April 03, 2011 7:11 PM