Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a dll created in c++ in vb.net?

Status
Not open for further replies.

Sidro

MIS
Sep 28, 2002
197
US
hi,
For this question I've made a dll in c++ and was wondering how to use it in vb.net. The name of the dll file is test.dll and here is its implementation.

#include<iostream>
using namespace std;

void print()
{
cout<<&quot;hello world!&quot;;
}

could someone show me how to set this up in vb.net?
And perhaps get vb to execute the print function and have it display,&quot;hello world,&quot; on a textbox.Thankx in advance!
 
See the Declare Statement
&quot;Used at module level to declare references to external procedures in a dynamic-link library (DLL).

[ <attrlist> ] [ Public | Private | Protected | Friend | Protected Friend ] [ Shadows ] _
Declare [ Ansi | Unicode | Auto ] [ Sub ] name Lib &quot;libname&quot; _
[ Alias &quot;aliasname&quot; ] [([ arglist ])]
-or-

[ <attrlist> ] [ Public | Private | Protected | Friend | Protected Friend ] [ Shadows ] _
Declare [ Ansi | Unicode | Auto ] [ Function ] name Lib &quot;libname&quot; _
[ Alias &quot;aliasname&quot; ] [([ arglist ])] [ As type ]


I do not know where to even start reading about how or ewen IF a C++ DLL can inteface with a .Net Framework TextBox. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Hi
You should be able to just add a reference to the dll in vb.net and use the imports keyword...

Regardless, if you want your TextBox to show the output from your dll's function
i.e.
Code:
Textbox1.Text = myobj.print()
then the function has to return a string...

Code:
std::string print()
{
  return &quot;hello world!&quot;;
}

I'm fairly sure that vb.net won't know what to do with the string unless it is a managed data type though.

You could try returning a ptr from the dll function and then using the Marshal class in vb.net to grab it:

Code:
Dim ptr As New IntPtr(myobj.print())
TextBox.Text = Marshal.PtrToString(ptr)

I'm not sure if that'll do the trick though and I haven't tried it myself, but it might get you headed in the right direction.



 
Psidro -

Besides what the others have mentioned, you need to make sure that your functions have #pragma declspec(dllexport) on them so that they get exported as Win32 functions, and not name-mangled C++ functions.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top