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

Interfacing C++ DLL and VB 1

Status
Not open for further replies.

PatrickIRL

Programmer
Jun 25, 2003
383
0
0
I come from a VB background and have created DLLs using VB but now I want to try using MSVC++.

I know this has been done to death here but I'm looking for something different.

I want to write a DLL in MSVC++ that I can use from VB6. All the examples I've seen here require a declaration in VB for every function that you need. What I would like to be able to do is reference the DLL in Project/References and then use the DOT operator to access the various functions.

Eg, if my DLL is called MyDLL then in VB I want to be able to use code such as MyDLL.Function1

Is this possible??

Thanks in advance,

Patrick
 
Patrick,

yup, that is possible if you make it a COM server. One possible way is to create a new project based on MFC App Wizard DLL. Make sure "Automation" is ticked in the following Dialog. Click finish. Right-click your <project>.cpp, select class wizard. On the Automation tab add a class. (new, take e.g. CCmdTarget). This is going to be your class, i.e. you will now add properties and methods to this class and use them later on the way you described it.

Volker/
 
Volker, just to follow up on this.

I created the project and added the class as stated above, so I've got a project called cdcpop and a class in that project called mcdcpop (I know, naming conventions!).

I reference the DLL in VB and I see the mcdcpop class but the function I have defined is not exposed. Any ideas??

The function is declared in mcdcpop.h as
Code:
int __declspec(dllexport) getLot1(void);

In mcdcpop.cpp I have a variable declared as such.
Code:
int lot1 = 99;

and a function ...
Code:
int getLot1(void){
    return lot1;
}

In the cdcpop.def file I put the function name in the EXPORTS area.

Any help appreciated.

Patrick
 
First of all, I forgot to mention something. When you create your new class select "Creatable by Type-ID" in the same box. Whatever name you enter there will be used as the com class name. Using the class wizard again (and selecting that new class) you can now add methods and properties. DON'T write your own function declarations as they won't work. And keep in mind that you're not going to declare dll functions in your vb code, you'll declare an object, something like
Code:
dim xx as MyServer.MyClass (The type-id you chose)
xx = createobject( "MyServer.MyClass" )
' or whatever the correct vb syntax is
xx.MethodName( ...)
Volker/
 
Volker,

Finally had the chance to try and it worked perfectly, thanks a million for your patience and assistance. Have a well deserved star.

Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top