Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PassThru driver on C# , Help #241

Open
pekovbg opened this issue Dec 27, 2024 · 2 comments
Open

PassThru driver on C# , Help #241

pekovbg opened this issue Dec 27, 2024 · 2 comments
Labels

Comments

@pekovbg
Copy link

pekovbg commented Dec 27, 2024

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!

@3F
Copy link
Owner

3F commented Dec 27, 2024

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:

play with docs (+video):

https://github.com/3F/DllExport/wiki/Examples-Part-2

Let me know if this doesn't help

@pekovbg
Copy link
Author

pekovbg commented Dec 27, 2024

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.

@pekovbg pekovbg closed this as completed Dec 27, 2024
@pekovbg pekovbg reopened this Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants