views:

288

answers:

3

INTRODUCTION

Hi,

I am very new to C++, is my 1st statement.

I have started initially with VC++ 2008 Express, I've notice 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 MSSQL 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::Block (I'm using last nightly buil, SVN 6181).

As compiler I choose to use GCC 3.4.5 (ported via MinGW 5.1.6), under 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 interprete 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.

MY QUESTION

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 finally my question:

  • how to use the ADO to access MSSQL database on a C++ program compiled with GCC (v3.4.5)?
A: 

The #import directive is not part of the standard C++ programming language. I don't know what (if anything) it does in GCC. The normal way to use a DLL is to include its header file(s) in your code and then link with its import library (the .LIB file). I've never programmed in ADO, but in ODBC you would

#include <sql.h>

and when compiling/linking

g++ mystuff.cpp -lodbc32

to get the import library.

Also, the version of the MinGW compiler you are using is very old. You can get a much more up-to-date one at http://tdragon.net/recentgcc.

anon
Hi,I tried to use (just few minutes before you post your answer) the sql.h header which is delivered with gcc. It look fine, I was little bit stubborn to stick with ADO just because I have already a small application which it is compiling just fine with MSVC++.I have installed also the cygwin, and I have tried to generate the .a library from the msado15.dll file. The nm bash command just said "No symbols in msado15.dll" so it seems that the dll symbols have been stripped.I saw the TDM-GCC web page, but I was discouraged to use it as it is "unofficial".I will try it.Thanks a lot.
Eugen Mihailescu
A: 

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.

Chris Becke
Hi,You are perfectly right, I already found this explanation on an article on Wikipedia. Thank you!
Eugen Mihailescu
A: 

Whats happens you try to import in very non-standard and MSVC specific way COM object...

AFAIK the support of COM with MSYS is far from begin perfect, search for: "component object model" and gcc in google.

I would suggest using FreeTDS or library like SOCI for communication with MS-SQL. It is also cross platform ;-)

Artyom
What has MSYS got to do with it?
anon
Thank you for such a quick reply.I was afraid that you will say this, Chris Becke and Neil Butterworth (see replies below) has pointed out the fundamental issue.Thank you, I will give a try with FreeTDS as I am a bit comfortable with it from some previous PHP "projects".
Eugen Mihailescu