Hex Artifact Content

Not logged in

Artifact 66565a0003521f7b828caa8975bf769301203af6:


0000: ef bb bf 70 72 69 76 61 74 65 20 69 6d 70 6f 72  ...private impor
0010: 74 20 77 69 6e 33 32 2e 61 6e 73 69 2e 77 69 6e  t win32.ansi.win
0020: 64 6f 77 73 3b 0a 0a 63 6c 61 73 73 20 57 69 6e  dows;..class Win
0030: 44 4c 4c 45 78 63 65 70 74 69 6f 6e 20 3a 20 45  DLLException : E
0040: 78 63 65 70 74 69 6f 6e 0a 7b 0a 09 70 72 69 76  xception.{..priv
0050: 61 74 65 20 74 68 69 73 28 20 63 68 61 72 5b 5d  ate this( char[]
0060: 20 6d 73 67 20 29 20 7b 20 73 75 70 65 72 28 6d   msg ) { super(m
0070: 73 67 29 3b 20 7d 0a 7d 0a 0a 63 6c 61 73 73 20  sg); }.}..class 
0080: 57 69 6e 44 4c 4c 0a 7b 0a 09 2f 2f 20 63 6f 6e  WinDLL.{..// con
0090: 73 74 72 75 63 74 6f 72 3a 0a 09 2f 2f 20 20 20  structor:..//   
00a0: 4c 6f 61 64 20 61 20 44 4c 4c 20 77 69 74 68 20  Load a DLL with 
00b0: 73 70 65 63 69 66 69 65 64 20 6e 61 6d 65 0a 0a  specified name..
00c0: 09 74 68 69 73 28 20 63 68 61 72 5b 5d 20 64 6c  .this( char[] dl
00d0: 6c 6e 61 6d 65 20 29 0a 09 7b 0a 09 09 63 68 61  lname )..{...cha
00e0: 72 20 6f 72 69 67 69 6e 61 6c 5f 63 75 72 5b 4d  r original_cur[M
00f0: 41 58 5f 50 41 54 48 5d 3b 0a 09 09 63 68 61 72  AX_PATH];...char
0100: 20 73 79 73 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a   sys[MAX_PATH];.
0110: 09 09 47 65 74 43 75 72 72 65 6e 74 44 69 72 65  ..GetCurrentDire
0120: 63 74 6f 72 79 28 4d 41 58 5f 50 41 54 48 2c 20  ctory(MAX_PATH, 
0130: 6f 72 69 67 69 6e 61 6c 5f 63 75 72 29 3b 0a 09  original_cur);..
0140: 09 47 65 74 53 79 73 74 65 6d 44 69 72 65 63 74  .GetSystemDirect
0150: 6f 72 79 28 73 79 73 2c 20 4d 41 58 5f 50 41 54  ory(sys, MAX_PAT
0160: 48 29 3b 0a 09 09 53 65 74 43 75 72 72 65 6e 74  H);...SetCurrent
0170: 44 69 72 65 63 74 6f 72 79 28 73 79 73 29 3b 0a  Directory(sys);.
0180: 09 09 68 61 6e 64 6c 65 20 3d 20 4c 6f 61 64 4c  ..handle = LoadL
0190: 69 62 72 61 72 79 28 20 64 6c 6c 6e 61 6d 65 20  ibrary( dllname 
01a0: 29 3b 0a 09 09 53 65 74 43 75 72 72 65 6e 74 44  );...SetCurrentD
01b0: 69 72 65 63 74 6f 72 79 28 6f 72 69 67 69 6e 61  irectory(origina
01c0: 6c 5f 63 75 72 29 3b 0a 09 09 69 66 28 20 68 61  l_cur);...if( ha
01d0: 6e 64 6c 65 20 69 73 20 6e 75 6c 6c 20 29 0a 09  ndle is null )..
01e0: 09 09 74 68 72 6f 77 20 6e 65 77 20 57 69 6e 44  ..throw new WinD
01f0: 4c 4c 45 78 63 65 70 74 69 6f 6e 28 20 64 6c 6c  LLException( dll
0200: 6e 61 6d 65 20 7e 20 22 20 6e 6f 74 20 66 6f 75  name ~ " not fou
0210: 6e 64 22 20 29 3b 0a 09 7d 0a 0a 09 73 74 61 74  nd" );..}...stat
0220: 69 63 20 57 69 6e 44 4c 4c 20 6c 6f 61 64 28 20  ic WinDLL load( 
0230: 63 68 61 72 5b 5d 20 6e 61 6d 65 20 29 0a 09 7b  char[] name )..{
0240: 0a 09 09 74 72 79 20 7b 0a 09 09 09 72 65 74 75  ...try {....retu
0250: 72 6e 20 6e 65 77 20 57 69 6e 44 4c 4c 28 6e 61  rn new WinDLL(na
0260: 6d 65 29 3b 0a 09 09 7d 20 63 61 74 63 68 28 57  me);...} catch(W
0270: 69 6e 44 4c 4c 45 78 63 65 70 74 69 6f 6e 20 65  inDLLException e
0280: 29 20 7b 20 72 65 74 75 72 6e 20 6e 75 6c 6c 3b  ) { return null;
0290: 20 7d 0a 09 7d 0a 0a 09 2f 2f 20 63 6c 6f 73 65   }..}...// close
02a0: 3a 0a 09 2f 2f 20 20 20 66 72 65 65 20 74 68 65  :..//   free the
02b0: 20 44 4c 4c 0a 0a 09 76 6f 69 64 20 63 6c 6f 73   DLL...void clos
02c0: 65 28 29 0a 09 7b 0a 09 09 69 66 28 20 68 61 6e  e()..{...if( han
02d0: 64 6c 65 20 29 0a 09 09 7b 0a 09 09 09 46 72 65  dle )...{....Fre
02e0: 65 4c 69 62 72 61 72 79 28 20 68 61 6e 64 6c 65  eLibrary( handle
02f0: 20 29 3b 0a 09 09 09 68 61 6e 64 6c 65 20 3d 20   );....handle = 
0300: 6e 75 6c 6c 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f  null;...}..}.../
0310: 2f 20 67 65 74 5f 61 70 69 3a 0a 09 2f 2f 20 20  / get_api:..//  
0320: 20 65 78 74 72 61 63 74 20 61 20 66 75 6e 63 74   extract a funct
0330: 69 6f 6e 20 77 69 74 68 20 73 70 65 63 69 66 69  ion with specifi
0340: 65 64 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65  ed name from the
0350: 20 44 4c 4c 2e 0a 09 2f 2f 20 20 20 6d 61 79 20   DLL...//   may 
0360: 72 65 74 75 72 6e 20 6e 75 6c 6c 2e 0a 0a 09 74  return null....t
0370: 65 6d 70 6c 61 74 65 20 67 65 74 5f 61 70 69 28  emplate get_api(
0380: 46 6e 54 29 0a 09 7b 0a 09 09 46 6e 54 20 67 65  FnT)..{...FnT ge
0390: 74 5f 61 70 69 28 20 63 68 61 72 5b 5d 20 61 70  t_api( char[] ap
03a0: 69 6e 61 6d 65 20 29 0a 09 09 09 69 6e 20 7b 20  iname )....in { 
03b0: 61 73 73 65 72 74 28 68 61 6e 64 6c 65 29 3b 20  assert(handle); 
03c0: 7d 0a 09 09 09 62 6f 64 79 0a 09 09 09 7b 0a 09  }....body....{..
03d0: 09 09 09 72 65 74 75 72 6e 20 63 61 73 74 28 46  ...return cast(F
03e0: 6e 54 29 20 47 65 74 50 72 6f 63 41 64 64 72 65  nT) GetProcAddre
03f0: 73 73 28 20 68 61 6e 64 6c 65 2c 20 61 70 69 6e  ss( handle, apin
0400: 61 6d 65 20 29 3b 0a 09 09 09 7d 0a 09 7d 0a 0a  ame );....}..}..
0410: 09 2f 2f 20 6c 6f 61 64 5f 72 65 73 6f 75 72 63  .// load_resourc
0420: 65 3a 0a 09 2f 2f 20 20 20 65 78 74 72 61 63 74  e:..//   extract
0430: 20 61 20 72 65 73 6f 75 72 63 65 0a 0a 09 76 6f   a resource...vo
0440: 69 64 2a 20 6c 6f 61 64 5f 72 65 73 6f 75 72 63  id* load_resourc
0450: 65 28 20 63 68 61 72 5b 5d 20 72 65 73 6e 61 6d  e( char[] resnam
0460: 65 2c 20 63 68 61 72 2a 20 74 79 70 65 20 29 0a  e, char* type ).
0470: 09 09 69 6e 20 7b 20 61 73 73 65 72 74 28 68 61  ..in { assert(ha
0480: 6e 64 6c 65 29 3b 20 7d 0a 09 09 62 6f 64 79 0a  ndle); }...body.
0490: 09 09 7b 0a 09 09 09 48 52 53 52 43 20 68 69 20  ..{....HRSRC hi 
04a0: 3d 20 46 69 6e 64 52 65 73 6f 75 72 63 65 28 20  = FindResource( 
04b0: 68 61 6e 64 6c 65 2c 20 72 65 73 6e 61 6d 65 2c  handle, resname,
04c0: 20 74 79 70 65 20 29 3b 0a 09 09 09 69 66 28 20   type );....if( 
04d0: 68 69 20 69 73 20 6e 75 6c 6c 20 29 0a 09 09 09  hi is null )....
04e0: 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0a 09 09  .return null;...
04f0: 09 48 47 4c 4f 42 41 4c 20 68 72 20 3d 20 4c 6f  .HGLOBAL hr = Lo
0500: 61 64 52 65 73 6f 75 72 63 65 28 20 68 61 6e 64  adResource( hand
0510: 6c 65 2c 20 68 69 20 29 3b 0a 09 09 09 69 66 28  le, hi );....if(
0520: 20 68 72 20 69 73 20 6e 75 6c 6c 20 29 0a 09 09   hr is null )...
0530: 09 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0a 09  ..return null;..
0540: 09 09 72 65 74 75 72 6e 20 4c 6f 63 6b 52 65 73  ..return LockRes
0550: 6f 75 72 63 65 28 20 68 72 20 29 3b 0a 09 09 7d  ource( hr );...}
0560: 0a 0a 09 2f 2f 20 6c 6f 61 64 5f 64 69 61 67 6c  ...// load_diagl
0570: 6f 67 3a 0a 09 2f 2f 20 20 20 73 70 65 63 69 61  og:..//   specia
0580: 6c 69 7a 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  lized version of
0590: 20 6c 6f 61 64 5f 72 65 73 6f 75 72 63 65 0a 0a   load_resource..
05a0: 09 76 6f 69 64 2a 20 6c 6f 61 64 5f 64 69 61 6c  .void* load_dial
05b0: 6f 67 28 20 63 68 61 72 5b 5d 20 72 65 73 6e 61  og( char[] resna
05c0: 6d 65 20 29 0a 09 7b 0a 09 09 72 65 74 75 72 6e  me )..{...return
05d0: 20 6c 6f 61 64 5f 72 65 73 6f 75 72 63 65 28 20   load_resource( 
05e0: 72 65 73 6e 61 6d 65 2c 20 52 54 5f 44 49 41 4c  resname, RT_DIAL
05f0: 4f 47 20 29 3b 0a 09 7d 0a 0a 09 70 72 69 76 61  OG );..}...priva
0600: 74 65 20 48 49 4e 53 54 41 4e 43 45 20 68 61 6e  te HINSTANCE han
0610: 64 6c 65 3b 0a 7d 0a                             dle;.}.