[C#] DLLimport (unload)

Discussion in 'Mixed Languages' started by FreeStyler, Aug 21, 2012.

  1. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,062
    60
    some keys are verified quicker than others, i think dependant on which xrm-ms is being used.
    i don't think the code has anything to do with it.

    if you are interested in my source (VBnet) shoot me a PM.
     
  2. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #22 CODYQX4, Aug 30, 2012
    Last edited: Apr 12, 2019
    .
     
  3. master131

    master131 MDL Novice

    Apr 12, 2011
    45
    22
    0
    #23 master131, Sep 9, 2012
    Last edited by a moderator: Apr 20, 2017
    You probably used some other method now but.... Using your example, you can call the function using something like this:
    Code:
    // In some function.....
    IntPtr dllHandle = LoadLibrary("ProductKeyUtilities.dll");
    IntPtr pAddressOfFunctionToCall = GetProcAddress(dllHandle, "PidGenX");
    var PidGenX = Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(PidGenXDelegate));
    PidGenX(productKeyHere, pKeyPathHere, MSPIDHere, OEMIDHere, ProductIDHere, DigitalProductIDHere);
    
    // Global declerations
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate int PidGenXDelegate(string ProductKey, string PkeyPath, string MSPID, string OEMID, IntPtr ProductID, IntPtr DigitalProductID, IntPtr DigitalProductID4);
    Of course, you'd only need to call LoadLibrary, and generate the delegate once instead of doing it each time you needed to use the function. Then when you're all done, cleanup using FreeLibrary.

    EDIT - Seems like Calistoga already bet me to it. :p You said it didn't work. It might because Calistoga's example did not explicitly specify the UnmanagedFunctionPointerAttribute. Make sure that your exported function's calling convention matches the once in the attribute too. Also make that you use the MarshalAs attributes on each string parameter in the delegate because the marshaller won't know what type of string you've used (eg. LPSTR, LPWSTR, etc). For example, if all the strings were LPWSTR, the delegate would look like this:
    Code:
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate int PidGenXDelegate([MarshalAs(UnmanagedType.LPWStr)] string ProductKey, [MarshalAs(UnmanagedType.LPWStr)] string PkeyPath, [MarshalAs(UnmanagedType.LPWStr)] string MSPID, [MarshalAs(UnmanagedType.LPWStr)] string OEMID, IntPtr ProductID, IntPtr DigitalProductID, IntPtr DigitalProductID4);