locked
How to p/invoke a native dll with LPTSTR as an input parameter RRS feed

  • Question

  • If the thread is posted previously,please let me know.Thanks a lot in advance.

    My question is in native unmanaged dll(DevDll.dll), function prototype is

    void GetDevName(LPTSTR p_DevName)
    {
     memset(p_DevName, 0x00, 32);
     strcpy(p_DevName, "Kin");
    }
    

    In C#, I attempt to find an proper data type corresponding to LPTSTR, function prototype declaration is as follows,

    [DllImport("DevDll.dll")]
    public static extern void GetLogicalDevName(StringBuilder str_DevName);
    
    
    

    such as string,stringbuilder,char[] ,intptr,also including key ref and out. Unfortunately all these data type are failed when invoke this method. Invoke failed, I mean, is not compiling time error or runtime error. After invoking function GetLogicalDevName, the value of str_DevName is not the expecting value.

    Anyone have some idea about or have experienced this question. Thanks a lot. ^_^

    Saturday, April 17, 2010 2:08 AM

Answers

  • In my case, byte[] is the right choice after trying some data types. So thanks for your attention,guys.
    • Marked as answer by enrico00 Sunday, April 18, 2010 2:04 AM
    Sunday, April 18, 2010 2:04 AM

All replies

  • Welcome to MSDN, Enrico.

    In my experience, you can pass that string with marshalling like...

        [DllImport("DevDll.dll")]
        public static extern void GetLogicalDevName([MarshalAs(UnmanagedType.LPTStr)]string str_DevName);
    
    Reference: http://msdn.microsoft.com/en-us/library/s9ts558h.aspx (Default Marshaling for Strings)
    Saturday, April 17, 2010 4:47 AM
  • Before calling the method, you need to set the size of the StringBuilder:

    StringBuilder sb = new StrnigBuilder(32);
    GetDevName(sb);

    • Proposed as answer by Tergiver Sunday, April 18, 2010 1:51 AM
    Saturday, April 17, 2010 10:27 AM
  • Welcome to MSDN, Enrico.

    In my experience, you can pass that string with marshalling like...

      [DllImport("DevDll.dll")]
    
      public static extern void GetLogicalDevName([MarshalAs(UnmanagedType.LPTStr)]string str_DevName);
    
    
    
    
    Reference: http://msdn.microsoft.com/en-us/library/s9ts558h.aspx (Default Marshaling for Strings)

    From the same link: "Strings are immutable and are not copied back from unmanaged memory to managed memory when the call returns."
    Saturday, April 17, 2010 10:32 AM
  • I have tried your suggestion,unfortunately after running function GetLogicalDevName str_DevName that I get is mess code. Maybe your suggestion is good,but in my case that is improper.

    Thanks a lot.

    Sunday, April 18, 2010 1:58 AM
  • Just the post I have said,I have try StringBuilder data type, but it dosen't get good result .Your suggestion maybe right, but not suit for my case.

    Thanks a lot.

    Sunday, April 18, 2010 2:00 AM
  • In my case, byte[] is the right choice after trying some data types. So thanks for your attention,guys.
    • Marked as answer by enrico00 Sunday, April 18, 2010 2:04 AM
    Sunday, April 18, 2010 2:04 AM
  • From the same link: "Strings are immutable and are not copied back from unmanaged memory to managed memory when the call returns."

    Thank you for the attention, Louis.

    According to the thread title, the string should be passed as an input string so we don't need any back-copy when the call returns.

    Am I right?

    Sunday, April 18, 2010 7:01 AM
  • Right. String should be correct. I think, if it doesn't work, that the CharSet is missing.
    Monday, April 19, 2010 10:19 AM
  • That's a really old post, but I have a similar case working with ref string and MarshalAs VBByRefStr

    [DllImport("DevDll.dll")]
    public static extern void GetLogicalDevName([MarshalAs(UnmanagedType.VBByRefStr)] ref string str_DevName);
    

    And to call it you can use:

    string devName = new string(' ', 32);
    GetLogicalDevName(ref devName);
    

    Saturday, June 18, 2016 9:02 PM