Skip to content

PassThru driver on C# , Help  #241

@pekovbg

Description

@pekovbg

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 Data tab:

(Remove this section after reading. Click Preview tab for convenience)

! Important

  1. Please use our wiki first. +Available Q&A list.

  2. 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

  1. 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+

  2. Make also sure you have all rights to publish any data (attached src, log, etc). Responsibility is solely on you.

  3. Try to use only GitHub for your attachments and screenshots instead of other places. It's free, it's enough.

  4. Please try to use basic formatting to avoid code dancing 🕺 across the page.

Thanks for reading!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions