>In a past article, we discussed how to call a DLL created by MS Visual C++ from a C++Builder project. This article reverses that scenario by illustrating how to create a DLL with C++Builder that you can call from a Visual C++ project. </P>0 D. y8 v7 D N& Z/ E% e7 D! b
>
>Introduction: why is this so difficult
>The situation becomes more complex when the EXE is compiled with Microsoft Visual C++. There are 3 main problems. First, BCB and MSVC do not agree on how functions should be named in the DLL. BCB uses one convention, and MSVC uses a different convention. Of course, the two conventions are not compatible. The naming problems are discussed in the article on how to use VC++ compiled DLLs in BCB. Table 1 summarizes how each compiler would export a function called MyFunction based on its calling convention. Notice that Borland prepends a leading underscore to exported __cdecl functions. MSVC does not. On the other hand, MSVC does expect __stdcall functions to be exported with a leading underscore and some garbage at the end. </P>0 J, j) H D4 N: I/ o: h% b
>Table 1: Visual C++ and C++Builder naming conventions</P>
>Calling convention VC++ name VC++ (DEF used) C++Builder Name
>The second problem is that Borland import libraries are not binary compatible with MSVC. The import library that BCB creates when you compile the DLL cannot be linked with MSVC. If you want to use implicit linking, then you need to create an MSVC import library. The other alternative is to switch to explicit linking (LoadLibrary and GetProcAddress). </P>
>The third problem is that you can't export C++ classes and member functions from your DLL if you want MSVC users to be able to call it. Well, that's not entirely true. Your DLL can export C++ classes and member functions, but MSVC will not be able to use them. The reason is that C++ member function names are mangled by the compiler. This mangled name ends up in the DLL. In order to call a mangled function in a DLL, you have to know how that function was mangled. Borland and Microsoft do not use the same name mangling scheme. As a reasult, MSVC cannot even see C++ classes and member functions in a Borland compiled DLL. </P>
> Note: ) d" z0 h5 |# P+ P, ^' l' `& X
>
>Summary of guidelines / i4 V$ E6 o; n; t
>Technique 1: Implicit linking0 `2 y! Z1 v7 W ~; k& F! U
> EXPORTS
>5- Add the DEF file to your BCB DLL project and rebuild it., X0 ~: }$ F U' H9 D3 l. p
>Technique 2: Explicit linking$ R$ h/ h) X& q1 W
>Technique 3: Implicit linking with a #define kludge% S: B9 }, ^3 n& y1 B. F
> #ifdef _MSC_VER/ x% k7 |* Z& p$ D; d! y
>5- Copy the DLL and the DLL header file to your MSVC project directory.
>Technique 4: Implicit linking with __stdcall functions1 ~# d4 {1 e1 u" H* \1 |
> Note:
>
>6 H: h- O+ `, M& \) S) v" T
>Example 1: Implicit linking . p% g) x# r* |1 i4 O: ~
>In this example, we will build a DLL with BCB that exports two functions: Foo and Bar. Both functions return an integer value. The functions look like this:
>We will then build a test EXE in MSVC that calls the Borland DLL.
>#ifdef __cplusplus
>#ifdef BUILD_DLL
>IMPORT_EXPORT int __cdecl Foo (int Value);
>#ifdef __cplusplus
>#endif" }8 ?' t5 O5 `6 K3 b: b| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |