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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Microsoft Visual C++ DLL in MS Visual Basic

Status
Not open for further replies.

t2hieu

Programmer
Dec 19, 2002
3
VN
I want to use some function in MS Visual C++ DLL file
How can i do it in MS Visual Basic?
 
The function you want to use in the DLL must be exported so that it is available to VB. (In the DEF file)

To call a DLL from VB you use the same syntax as making a windows API call. You declare the function, provide the path to the dll and and then you can use that function in code. I have a VC++ DLL I wrote that has a function called cnvrt. In my VB projects I declare it in the general declaration area like this:
Code:
Declare Function cnvrt lib "c:\u2d.dll" (ByVal fName as String) as Long
The path in quotes needs to be correct for wherever the DLL is actually placed. In my installs I just drop it on the root of the c drive. This assumes there is a c drive and all that- but I think you get the idea.

In the program itself you just call cnvrt. I have a little sub that does just that- it looks like this:
Code:
Public Function Unix2Dos(filename as String)
Dim l as long
l = cnvrt(filename)

End Function
This leaves out error handling, etc. Although- the thing you've got to watch if you are writing your own DLL is that often errors will take down your program and normal vb error handling wont be able to handle it. So you've got to make sure the C++ side handles its own issues. It can be a pain.

Using this method to enhance code is not too difficult and the payoff can be really nice in terms of ability and speed. Not to mention it can save you lots of time. The DLL I use above is one that I wrote to wrap around someone elses C++ code. I took a program I needed- tweaked it and then compiled it as a DLL so I could run it in a VB program that already existed- rather than rewrite all the work in VB.

Hope that helps,
Ron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top