2011년 6월 13일 월요일

Delphi에서 만든 DLL C++에서 사용하기

====================================================== 
Delphi Code 
====================================================== 

library HWPInfoDll; 
{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures
or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. } 
uses
  SysUtils,
  Classes,
  HwpInfo in 'xxx.pas'; 
{$E dll} 
{$R *.res} 
exports
  GetHWPInfo Name 'GetHWPInfo@Delphi';
begin 
end. 
unit HwpInfo; 
interface 
xxxx.pas
uses
  SysUtils,
  strUtils,
  Windows,
  Classes; 
  function GetHWPInfo(sFileFullName: PAnsichar):Integer; cdecl; 
  // 리턴값 0 = 몰라, 1= hwp2002, 2 = hwp2004, 3 = hwp2005, 4 = hwp2007
  function ConvertVersionName (sVer: string): integer ; 
implementation 
function GetHWPINfo(sFileFullName: PAnsichar):integer; cdecl; export;
begin
..
..
end; 




======================================================
C++ 코드
======================================================
선언부
#define Import(TYPE) extern "C" __declspec (dllimport) TYPE WINAPI
typedef int  (WINAPI *GetHWPInfo)(char *str); 
사용부
          // Delphi Dll Load
          GetHWPInfo GetHWPInfo1;
          CString sPath;
          char FullPath[255];
          int nHWPKind; 
          GetCurrentDirectory(255,FullPath); 
          sPath.Format("%s",FullPath); 
          sPath = sPath + "\\HWPInfo.dll"; 
          if (FILEEXISTS_S(sPath.GetBuffer(0))) {
            dprintf("HWPInfo.dll Path ok");
          } else {
            dprintf("HWPInfo.dll Path fail!");
          } 
          HMODULE h = LoadLibrary(sPath.GetBuffer(0)); 
          GetHWPInfo1 = (GetHWPInfo)GetProcAddress(h, "GetHWPInfo@Delphi");
          nHWPKind = GetHWPInfo1(PATH.GetBuffer(0));
          dprintf("HWP Kind = %d", nHWPKind); 
          FreeLibrary(h); 

64bit system32 폴더로 엑세스 하기

64비트 운영체제인 경우  아래 2개의 API를 이용 가능하다.

AAA : function(var OldValue: Pointer): BOOL; stdcall;
BBB : function(OldValue: Pointer): BOOL; stdcall;
pTmp : Pointer;

아래와 같이 선언하여 사용사용이 가능하다.
  AAA := GetProcAddress(GetModuleHandle(kernel32), 'Wow64DisableWow64FsRedirection');
  BBB := GetProcAddress(GetModuleHandle(kernel32), 'Wow64RevertWow64FsRedirection');


if AAA(pTmp) then begin
  // 정상적으로 System32폴더의 내용을 가져올수 있음.

end;


if BBB(pTmp) then begin
  // 64bit 윈도우 기본인 syswow64 폴더의 내용을 가져옴

end;