c++gccadomingwwindows-vista

Using MSADO15.DLL and C++ with MinGW/GCC on Windows Vista


I have started initially with VC++ 2008 Express. I've noticed that GCC becomes kind of standard, so I am trying to make the right steps event from the beginning.

I have written a piece of code that connects to a SQL Server server via ADO. On VC++, it's working like a charm by importing MSADO15.dll:

#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")

Because I am going to move from VC++ I was looking for an alternative (eventually multi-platform) IDE, so I stick (for this time) with Code::Blocks (I'm using the last nightly build, SVN 6181).

As the compiler, I choose to use GCC 3.4.5 (ported via MinGW 5.1.6), under Windows Vista.

I was trying to compile a simple "Hello, World!" application with GCC that use/import the same msado15.dll (#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")) and I was surprised to see a lot of compile-time errors.

I was expected that the #import compiler's directive will generate a library from "msado15.dll", so it can link to it later (link-edit time or whatever). Instead it was trying to read it as a normal file (like a header file, if you like), because it was trying to interpret each line in the DLL (which has a MZ signature):

Example:

Compiling: main.cpp
E:\MyPath\main.cpp:2:64: warning: extra tokens at end of #import directive
In file included from E:\MyPath\main.cpp:2:
c:\Program Files\Common Files\System\ADO\msado15.dll:1: error: stray '\144' in program
In file included from E:\MyPath\main.cpp:2:
c:\Program Files\Common Files\System\ADO\msado15.dll:1:4: warning: null character(s) ignored
c:\Program Files\Common Files\System\ADO\msado15.dll:1: error: stray '\3' in program
c:\Program Files\Common Files\System\ADO\msado15.dll:1:6: warning: null character(s) ignored
c:\Program Files\Common Files\System\ADO\msado15.dll:1: error: stray '\4' in program
...
and so on.

Well, it is obvious that under this version of GCC the #import directive does not do the expected job (perhaps #import is not supported anymore by GCC), so how can I use the ADO to access SQL Server database on a C++ program compiled with GCC (v3.4.5)?


Solution

  • GCC and MSVC use #import for different things. In GCC, #import is an objective-c variant of #include that only includes the header file once. In MSVC, #import triggers a Microsoft extension that builds a smart pointer implementation and header files from a COM type library.

    In GCC you are going to have to import the COM object the old fashioned way. Find the existing interface definitions or use a tool like OleSpy to generate the definitions from the tlb.