Created
November 13, 2011 14:32
-
-
Save 7shi/1362172 to your computer and use it in GitHub Desktop.
COMをC言語から呼び出す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Project1.dll: http://www.asahi-net.or.jp/~kv8s-yjm/another/yja002.htm */ | |
#define CINTERFACE | |
#include <stdio.h> | |
#include <windows.h> | |
#include <objbase.h> | |
CLSID CLSID_Class1 = | |
{0xd1890A6d,0xB213,0x11d4,{0xaf,0xc1,0x00,0xd0,0xb7,0xad,0x59,0xe8}}; | |
IID IID_Class1 = | |
{0xd1890a6c,0xb213,0x11d4,{0xaf,0xc1,0x00,0xd0,0xb7,0xad,0x59,0xe8}}; | |
typedef struct { | |
struct { | |
void *QueryInterface; | |
void *AddRef; | |
ULONG (_stdcall *Release)(void *_this); | |
void *GetTypeInfoCount; | |
void *GetTypeInfo; | |
void *GetIdsOfNames; | |
void *Invoke; | |
HRESULT (_stdcall *Add)(void *_this, int x, int y, int *ret); | |
} *lpVtbl; | |
} IClass1; | |
int main(int argc, char* argv[]) { | |
HMODULE hDll; | |
CoInitialize(NULL); | |
hDll = LoadLibraryA("Project1.dll"); | |
if (!hDll) printf("can not open dll\n"); else { | |
HRESULT (_stdcall *DllGetClassObject)(REFCLSID, REFIID, void **) = | |
(HRESULT (_stdcall *)(REFCLSID, REFIID, void**)) | |
GetProcAddress(hDll, "DllGetClassObject"); | |
if (!DllGetClassObject) printf("can not find proc\n"); else { | |
HRESULT hr; | |
IClassFactory *cf; | |
hr = (*DllGetClassObject)( | |
&CLSID_Class1, &IID_IClassFactory, (void **)&cf); | |
if (FAILED(hr)) printf("ERROR: %p\n", hr); else { | |
IClass1 *c1; | |
hr = cf->lpVtbl->CreateInstance( | |
cf, NULL, &IID_Class1, (void **)&c1); | |
if (FAILED(hr)) printf("ERROR: %p\n", hr); else { | |
int r; | |
c1->lpVtbl->Add(c1, 1, 2, &r); | |
printf("Add(1, 2) = %d\n", r); | |
c1->lpVtbl->Release(c1); | |
} | |
cf->lpVtbl->Release(cf); | |
} | |
} | |
FreeLibrary(hDll); | |
} | |
CoUninitialize(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add(1, 2) = 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment