Ray's playground

 

Understanding Platform Invocation Services(Chapter 1 of COM and .NET Interoperability) part2

 1 #ifdef MYCUSTOMDLL_EXPORTS
 2 #define MYCUSTOMDLL_API __declspec(dllexport)
 3 #else
 4 #define MYCUSTOMDLL_API __declspec(dllimport)
 5 #endif
 6 
 7 typedef struct _CAR
 8 {
 9     char* make;
10     char* color;
11 }CAR;
12 
13 typedef struct _CAR2
14 {
15     CAR theCar;
16     char* petName;
17 }CAR2;
18 
19 extern "C" MYCUSTOMDLL_API int AddNumbers(int x, int y);
20 extern "C" MYCUSTOMDLL_API int AddArray(int x[], int size);
21 extern "C" MYCUSTOMDLL_API void DisplayBetterCar(CAR2* theCar);
22 extern "C" MYCUSTOMDLL_API void GiveMeThreeBasicCars(CAR** theCars);
23 
24 class MYCUSTOMDLL_API CMiniVan
25 {
26 public:
27     CMiniVan()
28     {
29         m_numbKids = 52;
30     }
31     int DisplayNumberOfKids()
32     {
33         return m_numbKids;
34     }
35 private:
36     int m_numbKids;
37 };
38 
39 extern "C" MYCUSTOMDLL_API CMiniVan* CreateMiniVan();
40 extern "C" MYCUSTOMDLL_API void DeleteMiniVan(CMiniVan* obj);

 

 

Implementation:

 1 #include "MyCustomDLL.h"
 2 #include <windows.h>
 3 // For CoTaskMemAlloc().
 4 #include <objbase.h>
 5 
 6 extern "C" MYCUSTOMDLL_API int AddNumbers(int x, int y)
 7 {
 8     return x+y;
 9 }
10 
11 extern "C" MYCUSTOMDLL_API int AddArray(int x[], int size)
12 {
13     int sum = 0;
14     for(int i=0; i<size; i++)
15     {
16         sum += x[i];
17     }
18     return sum;
19 }
20 
21 extern "C" MYCUSTOMDLL_API void DisplayBetterCar(CAR2* theCar)
22 {
23     MessageBoxA(NULL, theCar->theCar.color, "Car Color", MB_OK);
24     MessageBoxA(NULL, theCar->theCar.make, "Car Make", MB_OK);
25     MessageBoxA(NULL, theCar->petName, "Car Pet Name", MB_OK);
26 }
27 
28 extern "C" MYCUSTOMDLL_API void GiveMeThreeBasicCars(CAR** theCars)
29 {
30     int numOfCars = 3;
31     *theCars = (CAR*)CoTaskMemAlloc(numOfCars * sizeof(CAR));
32 
33     char* carMakes[3= {"BMW""Ford""Viper"};
34     char* carColors[3= {"Green""Pink""Red"};
35 
36     CAR* pCurCar = *theCars;
37     for(int i=0; i<numOfCars; i++, pCurCar++)
38     {
39         pCurCar->color = carColors[i];
40         pCurCar->make = carMakes[i];
41     }
42 }
43 
44 extern "C" MYCUSTOMDLL_API CMiniVan* CreateMiniVan()
45 {
46     return new CMiniVan();
47 }
48 
49 extern "C" MYCUSTOMDLL_API void DeleteMiniVan(CMiniVan* obj)
50 {
51     delete obj;
52 }
53 
54 BOOL APIENTRY DllMain( HANDLE hModule, 
55  DWORD  ul_reason_for_call, LPVOID lpReserved)
56 {
57     switch (ul_reason_for_call)
58     {
59         case DLL_PROCESS_ATTACH:
60             MessageBoxA(NULL, "Dll is loading!""DllMain() says...", MB_OK);
61         break;
62         case DLL_PROCESS_DETACH:
63             MessageBoxA(NULL, "Dll is UNloading!""DllMain() says...", MB_OK);
64         break;
65     }
66     return TRUE;
67 }

 

 

Client
 1 #include <windows.h>
 2 #include <iostream>
 3 #include "MyCustomDLL.h"
 4 using namespace std;
 5 
 6 int main(int argc, char* argv[])
 7 {
 8     typedef int (*PFNADDNUMBERS) (intint);
 9     PFNADDNUMBERS pfnAddMethod;
10 
11     typedef int (*PFNDISPLAYBETTERCAR) (CAR2*);
12     PFNDISPLAYBETTERCAR pfnDisplayBetterCar;
13 
14     HINSTANCE dllHandle = NULL;
15     dllHandle = LoadLibraryA("MyCustomDLL.dll");
16 
17     if(dllHandle != NULL)
18     {
19         pfnAddMethod = (PFNADDNUMBERS)GetProcAddress(dllHandle, "AddNumbers");
20         if(pfnAddMethod != NULL)
21         {
22             int result = pfnAddMethod(100100);
23             cout << "100 + 100 is " << result << endl;
24         }
25 
26         CAR2 myCar;
27         myCar.petName = "dog";
28         myCar.theCar.make = "make";
29         myCar.theCar.color = "Red";
30 
31         pfnDisplayBetterCar = (PFNDISPLAYBETTERCAR)GetProcAddress(dllHandle, "DisplayBetterCar");
32         if(pfnDisplayBetterCar != NULL)
33         {
34             pfnDisplayBetterCar(&myCar);
35         }
36 
37         FreeLibrary(dllHandle);
38     }
39 
40     cin.get();
41 
42     return 0;
43 }

 

 

 

posted on 2010-03-09 22:30  Ray Z  阅读(275)  评论(0编辑  收藏  举报

导航