Re: PInvoke + Prevent Piracy

Girish Jain <[email protected]> Sun, 3 May 2009 16:06:43 +0000
Newsgroups gmane.comp.windows.devel.dotnet.advanced
Message-ID <[email protected]>
Hi Ben,
 
Your shot has hit the right target... You are correct. I have modified the signature and is able to call the method correctly and the method call succeeds. Now, the next issue I am facing is with retrieving the returned data.
 
The method populates the structure. When I try to convert the structure pointer back to a managed type instance using Marshal.PtrToStructure, it does not succeed and rather gives me some FatalEngineExecution exception. I dont have experience with COM programming and therefore, having issues with COM Interop with .NET.
 
I am writing this mail from home and dont have the latest working code as of now. I shall post the latest code at the first possible opportunity.
 
Thanks a lot
 
 
Cheers,
Girish Jain

----------------------------------------
> Date: Wed, 29 Apr 2009 20:58:23 +0200
> From: [email protected]
> Subject: Re: [ADVANCED-DOTNET] PInvoke + Prevent Piracy
> To: [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("",
> 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
_________________________________________________________________
Live Search extreme As India feels the heat of poll season, get all the info you need on the MSN News Aggregator
http://news.in.msn.com/National/indiaelections2009/aggregator/default.aspx
===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives