ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Creating a Windows DLL with Visual Basic
Subject:   can vb6 class/form be in windows dll ?
Date:   2005-05-19 07:26:10
From:   JohnDrinkall
Response to: can vb6 class/form be in windows dll ?

Yes you can. Okay a really simple example.


Create a VB dll (This is Sample.SampleClass.


Option Explicit


Public Sub SayHello(ByVal strVal As String)
MsgBox strVal
End Sub


Since you didn't specify which DLL type, I'm plumping for the regular kind. From memory I think that Active-x's dlls would be easier, but I've not had time to check.


Start C++ (I'm using V7, but you can use V6, just the menu options are different and I've forgottern where they are for 6.)


Create a windows Dll with MFC support called TestConsumer. This is important since we need the COleDispatchDriver class.


Add a new class, select implement MFC from type library and select your VB Dll that you have created (or another object)


It generates the following


class CSampleClass : public COleDispatchDriver
{
public:
CSampleClass(){} // Calls COleDispatchDriver default constructor
CSampleClass(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
CSampleClass(const CSampleClass& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}


// Attributes
public:


// Operations
public:



// _SampleClass methods
public:
void SayHello(LPCTSTR strVal)
{
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x60030000, DISPATCH_METHOD, VT_EMPTY, NULL, parms, strVal);
}


// _SampleClass properties
public:


};



Export an entry point by adding it into the def file


LIBRARY "TestConsumer"


EXPORTS
; Explicit exports can go here
TestDll



Add the function into the DLL


void __stdcall TestDll(IDispatch* IDisp)
{
CSampleClass cSamp(IDisp);
cSamp.SayHello("Hello there");
cSamp.DetachDispatch(); // Use this so that when the C class falls out of scope it won't take the VB object with it.


}


Build and place in the VB app directory


Write a sample VB app


Private Declare Sub TestDll Lib "TestConsumer" (ByVal oObject As Object)


Private Sub Test_Click()


Dim oSample As sample.SampleClass
Set oSample = New sample.SampleClass

oSample.SayHello "Test"

TestDll oSample

oSample.SayHello "Back again"

Set oSample = Nothing

End Sub



And the message boxes you should see are "Test", "Hello There", and "Back Again" - easy eh?



If you have any questions drop me a mail at JohnDrinkall@yahoo.com


Cheers


John


1 to 1 of 1
  1. can vb6 class/form be in windows dll ?
    2005-07-08 07:04:45  JohnDrinkall [View]

    • can vb6 class/form be in windows dll ?
      2007-03-29 19:19:32  draftsman [View]

    • can vb6 class/form be in windows dll ?
      2007-03-29 19:16:22  draftsman [View]

1 to 1 of 1