Re: PInvoke + Prevent Piracy

Benjamin Schwehn <[email protected]> Wed, 29 Apr 2009 20:58:23 +0200
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
On 29.04.2009 17:44, Girish Jain wrote:
>    static class WinSDK
>      {
>          [DllImport("Imagehlp.dll", SetLastError=true)]
>          static public extern bool ImageAddCertificate(SafeFileHandle HANDLE, ref WINCERTIFICATE certData, out long index);
>          [DllImport("Imagehlp.dll", SetLastError = true)]
>          static public extern bool ImageGetCertificateData(SafeFileHandle FileHandle, long CertificateIndex, out IntPtr CertData, out long RequiredLength);
>      }
>      struct WINCERTIFICATE
>          {
>          public UInt32 dwLength;
>          public int wRevision;
>          public int wCertificateType;
>          public byte[] bCertificate;
>          }
>
> Unmanaged counterpart
> --------------------------------------------------------
> BOOL
> IMAGEAPI
> ImageGetCertificateData(
>      IN      HANDLE  FileHandle,
>      IN      DWORD   CertificateIndex,
>      OUT     LPWIN_CERTIFICATE Certificate,
>      IN OUT  PDWORD  RequiredLength
>      );
>
> typedef struct _WIN_CERTIFICATE
> {
>      DWORD       dwLength;
>      WORD        wRevision;
>      WORD        wCertificateType;   // WIN_CERT_TYPE_xxx
>      BYTE        bCertificate[ANYSIZE_ARRAY];
> } WIN_CERTIFICATE, *LPWIN_CERTIFICATE;


Shot in the dark:

shouldn't pdword pinvoked be UInt32 or IntPtr, not long?

A quick test with the P/Invoke interop assistent[1] converts this:

BOOL
PVOID
ImageGetCertificateData(
     IN      HANDLE  FileHandle,
     IN      DWORD   CertificateIndex,
     OUT     PVOID Certificate,
     IN OUT  PDWORD  RequiredLength
     );


to this:

     [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", 
EntryPoint="ImageGetCertificateData")]
public static extern  System.IntPtr 
ImageGetCertificateData(System.IntPtr FileHandle, uint CertificateIndex, 
System.IntPtr Certificate, ref uint RequiredLength) ;

Same with WIN_CERTIFICATE, WORD -> ushort:


public struct WIN_CERTIFICATE {

     /// DWORD->unsigned int
     public uint dwLength;

     /// WORD->unsigned short
     public ushort wRevision;

     /// WORD->unsigned short
     public ushort wCertificateType;

     /// BYTE[1]
 
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, 
SizeConst=1, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
     public byte[] bCertificate;
}


Apart from that I agree with Adam, seems to be too much trouble for an 
easily bypassed protection.


Ben

[1]http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120

===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives