0

I am trying to Access a third Party .dll(unmanaged) in C#. My Methods are as shown below

static class NativeMethods
{    
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);

}
class VTCCAN
{
       [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
       private delegate int CAN_Transmission(ref can_msg message);
        static readonly string dllfile = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\VTC1010_CAN_Bus.dll";
        public int SendCAN(ref can_msg msg)
        {
            IntPtr pDll = NativeMethods.LoadLibrary(dllfile);
            if (pDll == IntPtr.Zero)
            {
                MessageBox.Show("Loading Failed");
            }
            IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "CAN_Transmission");
            CAN_Transmission sendCAN = (CAN_Transmission)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CAN_Transmission));
            int result = sendCAN(ref msg);
            bool iresult = NativeMethods.FreeLibrary(pDll);
            return result;
        }
    }
}

I am getting an error when I access the function 'SendCAN' the error is "Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I looked at some suggestions but its not applicable for my code. Is there something wrong here. Please suggest. I cant find answer for my case. (64 bit Windows 7 OS)

The assosiated Struct as provided by Abe

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Ansi)]
public struct can_msg
{
    /// unsigned short
    public short ide;

    /// unsigned int
    public int id;

    /// unsigned short
    public short dlc;

    /// unsigned char[100]
    [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = NativeConstants.CAN_MSG_DATA_LEN)]
    public string data;

    /// unsigned short
    public short rtr;
}
  • You should post the associated `struct` as well. – aybe Aug 27 '15 at 13:49
  • 1
    For me the GetProcAddress have this annotation: `[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]`. LoadLibrary have this:`[DllImport("kernel32", SetLastError = true)]`. Also You can write `CAN_Transmission sendCAN = (CAN_Transmission)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall));`. I would also add unsafe keyword before the delegate – ntohl Aug 27 '15 at 13:49
  • Have You tried dumpbin for the dll? Is `CAN_Transmission` visible for .NET? – ntohl Aug 27 '15 at 13:53
  • BTW, if you use the `struct` I posted yesterday and using `ByValTStr` for `data`, make sure that `CharSet` and `SizeConst` are correctly defined for struct and field (https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype(v=vs.90).aspx). If all else fails, you might want to try `LPStr` for the field and eventually `Pack=1` for the struct. – aybe Aug 27 '15 at 13:54
  • What is the dll compiled with? – ntohl Aug 27 '15 at 13:57
  • @ntohl, the syntax is not right for initializing sendCAN – Abhishek Kumar Aug 27 '15 at 14:01
  • @AbhishekKumar I had this, that was working> `unsafe public delegate void *GetSomething();` and `GetSomething getSomething = (GetSomething)Marshal.GetDelegateForFunctionPointer(pfn);`, where pfn is the process address. Later I use the getSomething like a method: `int* variable = (int*)getSomething();` – ntohl Aug 27 '15 at 14:04
  • The .dll is compiled with C++. – Abhishek Kumar Aug 27 '15 at 14:11
  • Which C++ compiler. cygwin? mingw? – ntohl Aug 27 '15 at 14:13
  • The.dll itself is provided a 3rd Party. How can I check it? Please guide. – Abhishek Kumar Aug 27 '15 at 14:16
  • I added the struct, please have a look now. – Abhishek Kumar Aug 27 '15 at 14:20
  • First start a Developer Command Prompt for VS..., and run a `dumpbin.exe /EXPORTS VTC1010_CAN_Bus.dll`. Post the result. – ntohl Aug 27 '15 at 14:22
  • Not a recognised command – Abhishek Kumar Aug 27 '15 at 14:35
  • Check theese options> [link](http://stackoverflow.com/questions/477387/cannot-find-dumpbin-exe) – ntohl Aug 27 '15 at 14:44
  • Thanx for ure support mate!!! @ntohl – Abhishek Kumar Aug 27 '15 at 14:58

0 Answers0