-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
dclient.d
132 lines (108 loc) · 3.03 KB
/
dclient.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Client for IHello
* Heavily modified from:
*/
/*
* SELFREG.CPP
* Server Self-Registrtation Utility, Chapter 5
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : [email protected]
* Compuserve: >INTERNET:[email protected]
*/
import core.stdc.stdio;
import core.stdc.stdlib;
import core.sys.windows.com;
import core.sys.windows.winbase;
import core.sys.windows.windef;
GUID CLSID_Hello = { 0x30421140, 0, 0, [0xC0, 0, 0, 0, 0, 0, 0, 0x46] };
GUID IID_IHello = { 0x00421140, 0, 0, [0xC0, 0, 0, 0, 0, 0, 0, 0x46] };
interface IHello : IUnknown
{
extern (Windows) :
int Print();
}
int main()
{
DWORD dwVer;
HRESULT hr;
IHello pIHello;
// Make sure COM is the right version
dwVer = CoBuildVersion();
if (rmm != HIWORD(dwVer))
{
printf("Incorrect OLE 2 version number\n");
return EXIT_FAILURE;
}
hr=CoInitialize(null); // Initialize OLE
if (FAILED(hr))
{
printf("OLE 2 failed to initialize\n");
return EXIT_FAILURE;
}
printf("OLE 2 initialized\n");
if (dll_regserver("dserver.dll", 1) == 0)
{
printf("server registered\n");
hr=CoCreateInstance(&CLSID_Hello, null, CLSCTX_ALL, &IID_IHello, cast(void**)&pIHello);
if (FAILED(hr))
{
printf("Failed to create object x%x\n", hr);
}
else
{
printf("Object created, calling IHello.Print(), IHello = %p\n", pIHello);
// fflush(stdout);
pIHello.Print();
pIHello.Release();
}
CoFreeUnusedLibraries();
if (dll_regserver("dserver.dll", 0))
printf("server unregister failed\n");
}
else
printf("server registration failed\n");
// Only call this if CoInitialize worked
CoUninitialize();
return EXIT_SUCCESS;
}
/**************************************
* Register/unregister a DLL server.
* Input:
* flag !=0: register
* ==0: unregister
* Returns:
* 0 success
* !=0 failure
*/
extern (Windows) alias HRESULT function() pfn_t;
int dll_regserver(const (char) *dllname, int flag)
{
char *fn = flag ? cast(char*) "DllRegisterServer"
: cast(char*) "DllUnregisterServer";
int result = 1;
pfn_t pfn;
HINSTANCE hMod;
if (SUCCEEDED(CoInitialize(null)))
{
hMod=LoadLibraryA(dllname);
printf("hMod = %p\n", hMod);
if (hMod)
{
printf("LoadLibraryA() %s\n", (flag ? "registered".ptr : "unregistered".ptr));
pfn = cast(pfn_t) GetProcAddress(hMod, fn);
printf("pfn = %p, fn = '%s'\n", pfn, fn);
if (pfn && SUCCEEDED((*pfn)()))
{
printf("successfully called %s\n", fn);
result = 0;
}
printf("CoFreeLibrary()\n");
CoFreeLibrary(hMod);
printf("CoUninitialize()\n");
CoUninitialize();
}
}
return result;
}