This repository has been archived by the owner on Apr 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbj.com.unsafe.cs
103 lines (93 loc) · 2.68 KB
/
dbj.com.unsafe.cs
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
/*
* DBJ COM Magic
* (c) 2001 -2013 by Dusan B. Jovanovic
*/
//#define TESTING
namespace dbj
{
namespace com
{
using System ;
using System.Reflection ;
using System.Runtime.InteropServices ;
/// <summary>
/// The unsafe version of the dbj.com.dll
/// </summary>
internal sealed unsafe class dllUnsafe
{
public enum retval { S_OK = 0 };
/*
typedef struct _GUID
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID, UUID;
*/
[StructLayout(LayoutKind.Explicit)]
unsafe struct _GUID
{
[FieldOffset(0)]public ulong Data1;
[FieldOffset(0)]public ushort Data2;
[FieldOffset(0)]public ushort Data3;
[FieldOffset(0)]public char [] Data4 ; // len = 8
} ;
/*
WINOLEAPI ProgIDFromCLSID(REFCLSID clsid,LPOLESTR * lplpszProgID);
*/
[DllImport("OLE32.DLL", EntryPoint="ProgIDFromCLSID", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
static unsafe extern int ProgIDFromCLSID( ref dllUnsafe._GUID class_id, char * progid);
// wrapper for the above
public static unsafe string progid_from_clsid ( Guid class_id )
{
char * progid_ = stackalloc char [256] ;
_GUID _guid = guid2guid( class_id );
int retvalue = dllUnsafe.ProgIDFromCLSID( ref _guid , progid_ );
switch( retvalue )
{
case (int)retval.S_OK : return new string(progid_,0,39) ;
default :
// int err_code = Marshal.GetLastWin32Error() ;
System.ComponentModel.Win32Exception myEx =
new System.ComponentModel.Win32Exception(retvalue);
System.Diagnostics.Debug.WriteLine("ERROR Message:\t" + myEx.Message);
System.Diagnostics.Debug.WriteLine("ERROR Code:\t" + myEx.ErrorCode);
System.Diagnostics.Debug.WriteLine("ERROR Source:\t" + myEx.Source);
break ;
}
return string.Empty ;
}
/*
* TBD -- obviously
*/
static unsafe dllUnsafe._GUID guid2guid ( Guid managed_guid )
{
byte [] bits = managed_guid.ToByteArray() ;
dllUnsafe._GUID retval = new _GUID() ;
return retval ;
}
#if TESTING
public static void test ( params string [] args )
{
foreach( string clsid in args )
{
System.Diagnostics.Debug.WriteLine( "For CLSID { " + clsid + "}" ) ;
try
{
System.Diagnostics.Debug.WriteLine("PROGID = " +
dllUnsafe.progid_from_clsid( new Guid(clsid) )
) ;
}
catch (Exception x)
{
System.Diagnostics.Debug.WriteLine("dll.test() Exception") ;
System.Diagnostics.Debug.WriteLine(x) ;
}
}
}
#endif
}
}
}