Fog Creek Software
Discussion Board




Passing ADO Connection pointer from VB to VC

Hi,

I made a program thats uses an MS acces database.
It is made of two parts: a VB part (the GUI) an a dll written in C++
with MS VC 6.
Because of the problem 216925 (see http://support.microsoft.com/defaul...kb;EN-US;216925), i want to use only one ADODB.Connection object between my VB and VC code.
I open the connection within the VB code, and i want to pass to the dll a pointer to the opened connection, but i don't know how to do.

Could anyone help me ?

P.S. Sorry for my poor english

Alain
Tuesday, June 08, 2004


I don't know if you actually mean to ask this question, but here's what I can help you with.

You'll first import a reference to the ADO library on your machine,

#import "ThePathToYourADOLibrary\msadoXX.dll"

And in the header itself, you'll also export the function you want to include in the public interface of your DLL,

__declspec(dllexport) int DoSomethingWithTheConnection(ADODB::Connection*)

Then you could implement this function to do something in one of your source files,

__declspec(dllexport) int DoSomethingWithTheConnection(DoSomethingWithTheConnection(ADODB::_ConnectionPtr conn)
{
        conn->Open("Provider=MSDASQL.1;...blah...blah..Data Source=...blah");
//Do more blah...
}

Sathyaish Chakravarthy
Tuesday, June 08, 2004


I thought I'll just wrap the code:


#import "ThePathToYourADOLibrary\msadoXX.dll"

__declspec(dllexport) int
DoSomethingWithTheConnection(ADODB::Connection*)

__declspec(dllexport) int
DoSomethingWithTheConnection
(ADODB::_ConnectionPtr conn)
{
        conn->Open("Provider=MSDASQL.1;...blah...blah..
    Data Source=...blah");
//Do more blah...
}

And from VB, you'll set a reference to the ADO library and just say:

(The declare will be private in an object module, public otherwise)

Private Declare Function _
DoSomethingWithTheConnection Lib "ThePathToYourDLL\MyDll.dll" _
(ByRef con As ADODB.Connection) As Long


Dim Con as ADODB.Connection
Set Con = New Connection
Call DoSomethingWithTheConnection(Con)

Sathyaish Chakravarthy
Tuesday, June 08, 2004

It might be easier to expose a COM interface to pass the Connection from VB to C++.  I did something similar in an ASP project.

Brian
Tuesday, June 08, 2004

I tried to do what you said (#import "ThePathToYourADOLibrary\msadoXX.dll"

And in the header itself, you'll also export the function you want to include in the public interface of your DLL,

__declspec(dllexport) int DoSomethingWithTheConnection(ADODB::Connection*) ), but i got an error message from VC++ when i tried to compile: "ADODB' : is not a class or namespace name", and I don't know how to make it work.

Alain

Alain
Wednesday, June 09, 2004


I don't know what could be the problem but here's a working piece of code I just whipped out, look:

[CODE]
#include <windows.h>
#import "C:\Program Files\Common Files\System\ADO\msado15.dll"

extern "C" __declspec(dllexport) int DoSomething(ADODB::_ConnectionPtr con)
{
    con->ConnectionString = "Foo";
    MessageBox(0,con->ConnectionString,"Foo",MB_OK);
    return 0;
}
[/CODE]

[DEF FILE]
LIBRARY "Foo"
DESCRIPTION Foo bar
EXPORTS
    DoSomething @1
[/DEF FILE]

Sathyaish Chakravarthy
Wednesday, June 09, 2004

*  Recent Topics

*  Fog Creek Home