PassThru driver on C# , Help
Hello. I try to Make PassThru driver on C# . for example i have exported function :
[DllExport(CallingConvention.StdCall,ExportName = "PassThruReadVersion")]
public static J2534Err test_version(int deviceId, ref IntPtr firmwareVersion, ref IntPtr dllVersion, ref IntPtr apiVersion)
{
firmwareVersion = Marshal.AllocHGlobal(80);
firmwareVersion = Marshal.StringToHGlobalAnsi(dll_string);
Marshal.FreeHGlobal(firmwareVersion);
return J2534Err.STATUS_NOERROR;
}
but i have the problem , the application cannot read string from dll . I test with c# Wrapper , but i don't receive random string. Can you help me with this case ?
The Wrapper read code
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int PassThruReadVersion(int deviceId, IntPtr firmwareVersion, IntPtr dllVersion, IntPtr apiVersion);
public PassThruReadVersion ReadVersion;
public J2534Err ReadVersion(int deviceId, ref string firmwareVersion, ref string dllVersion, ref string apiVersion)
{
J2534Err returnValue = J2534Err.ERR_FAILED;
try
{
IntPtr pFirmwareVersion = Marshal.AllocHGlobal(120);
IntPtr pDllVersion = Marshal.AllocHGlobal(120);
IntPtr pApiVersion = Marshal.AllocHGlobal(120);
returnValue = (J2534Err)m_wrapper.ReadVersion(deviceId, pFirmwareVersion, pDllVersion, pApiVersion);
if (returnValue == J2534Err.STATUS_NOERROR)
{
firmwareVersion = Marshal.PtrToStringAnsi(pFirmwareVersion);
dllVersion = Marshal.PtrToStringAnsi(pDllVersion);
apiVersion = Marshal.PtrToStringAnsi(pApiVersion);
}
Marshal.FreeHGlobal(pFirmwareVersion);
Marshal.FreeHGlobal(pDllVersion);
Marshal.FreeHGlobal(pApiVersion);
}
catch(Exception er) { Console.WriteLine(er.Message); Console.WriteLine(er.StackTrace); Debug.WriteLine(er.Message); Debug.WriteLine(er.StackTrace); }
return returnValue;
}
On c++ work , for example :
PANDAJ2534DLL_API long PTAPI PassThruReadVersion(unsigned long DeviceID, char *pFirmwareVersion, char *pDllVersion, char *pApiVersion) {
#pragma EXPORT
//if (!loadedFine) return ERR_DEVICE_NOT_CONNECTED;
//auto res = LocalReadVersion(DeviceID, pFirmwareVersion, pDllVersion, pApiVersion);
auto res = STATUS_NOERROR;
strcpy(pFirmwareVersion,"1.0.0");
strcpy(pDllVersion, "1.0.0");
strcpy(pApiVersion, "1.0.0");
return res;
}
. .
The question is related to:
-
DllExport -version: - Copy-paste from
Datatab:
⚠ (Remove this section after reading. Click Preview tab for convenience)
! Important
-
Please try to use MSDN, stackoverflow, and other relevant places for understanding common practice with P/Invoke, scalar & unmanaged native types, marshaling, .net-domains, multithreading, ... ~something other. Because this is not directly related to our project to teach programming. We can try to help anyway, but please have a conscience.
Before ask anything
-
Do not put inside message any very long text data ( ~10 Kb+ time for attachments ). Means only as file (text-based, or zip, etc). Because of notifications through email. It's really ... 100K+
-
Make also sure you have all rights to publish any data (attached src, log, etc). Responsibility is solely on you.
-
Try to use only GitHub for your attachments and screenshots instead of other places. It's free, it's enough.
-
Please try to use basic formatting to avoid code dancing 🕺 across the page.
Thanks for reading!
Hello,
remove ref from ref IntPtr that's like over through over here and ... in general your wrapper is too complicated and not safe in case of possible memory leaks; use try/finally for FreeHGlobal etc.
Okay, tl;dr, let's try to look at something related:
play with tests:
- https://github.com/3F/DllExport/blob/master/src/DllExport/assets/NetfxAsset/Basic.cs
- https://github.com/3F/DllExport/blob/master/src/DllExport/UnitedTest/NetfxAssetBasicTest.cs
play with docs (+video):
https://github.com/3F/DllExport/wiki/Examples-Part-2
Let me know if this doesn't help
Thanks for the quick reply, I tried it without ref, then I don't get any data at all, with ref I get a random string. Thanks for the additional materials, before writing to you I read them and tried them.