dddds

 
 
 

Answer passing string from c++ dll to c#

  • Tuesday, September 27, 2011 7:24 AM
    Avatar of Elvinj
    Elvinj
    Avatar of Elvinj

    Elvinj

    0
    Recent Achievements 2 0 0
    First Marked Answer First Forums Reply
    0
     
      Has Code

    Hi, I'm writing a program for Windows Mobile 6.5 pro devices.

    I want to pass a string value generated in c++ dll to C#, but it always return null. I had try to pass back integral, and it success. may i know what am I missing in the programming code?

    my c++ dll: (WMDLL.dll)

    extern "C"
    {
    	__declspec(dllexport) int passInt()
    	{
    		return 1234;
    	}
    }
    
    extern "C"
    {
    	__declspec(dllexport) char* passStr()
    	{
    		return "4545";
    	}
    }
    
    

     


    my c#

    public class Win321
    {
      [DllImport("WMDLL.dll")]
      public static extern String getStr();
    
      [DllImport("WMDLL.dll")]
      public static extern int getInt();
    }
    


    calling code:

    MessageBox.Show("Int = " + Win321.getInt);
    
    MessageBox.Show("Str = " + Win321.getStr);
    

    Result:

    Int = 1234

    Str =

     




    • Edited by Elvinj Tuesday, September 27, 2011 7:28 AM
    •  

Answers

  • Wednesday, September 28, 2011 3:28 AM
    Avatar of Ilya Tumanov
    Ilya Tumanov
    Avatar of Ilya Tumanov

    Ilya Tumanov

    MSFT

    86,910
    Recent Achievements 11 5 3
    Forums Replies VI First Helpful Vote Proposed Answerer I
    (MSFT)
    86,910
     
      Answer
    Look into standard C library for working with strings as well as into secure versions (strcpy(),  wcscpy_s(), etc.)
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, September 29, 2011 4:15 PM
    Avatar of ctacke
    ctacke
    Avatar of ctacke

    ctacke

    OpenNETCF Consulting

    MVP

    1,270
    Recent Achievements 9 0 0
    Forums Replies II First Forums Spam Report Forums Answerer II
    OpenNETCF Consulting
    (MVP)
    1,270
     
      Answer
    As Ilya points out, you should not return a string from a C function.  Here's a bit more of a description as to why.  It's important to understand the "why" of this.

All Replies

  • Tuesday, September 27, 2011 8:42 AM
    Avatar of vinothmdu2001
    vinothmdu2001
    Avatar of vinothmdu2001

    vinothmdu2001

    e con systems India Pvt Ltd

    180
    Recent Achievements 7 0 0
    First Answer Confirmed Forums Answerer I First Helpful Vote
    e con systems India Pvt Ltd
    180
     
      Has Code

     

     

    Hi,

       Instead of return string value use the pass by reference technique. you should allocate buffer on higher level and pass it, then fill that buffer on lower level like this.

    [System.Runtime.InteropServices.DllImportAttribute("WMDLL.dll", EntryPoint = "getStr")]
            [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
            public static extern bool getStr(StringBuilder strValue);
    

    StringBuilder strValue = new StringBuilder(250);
                        if (getstr(strValue))
    {
       ....
    }
    else
    {<br/>...<br/> }<br/><br/>
    

    In C++

    BOOL getstr(TCHAR *strValue)

    {

      //assign the strValue here

    }


    Thanks and Regards, vinothkumar.A
  • Tuesday, September 27, 2011 12:42 PM
    Avatar of Ilya Tumanov
    Ilya Tumanov
    Avatar of Ilya Tumanov

    Ilya Tumanov

    MSFT

    86,910
    Recent Achievements 11 5 3
    Forums Replies VI First Helpful Vote Proposed Answerer I
    (MSFT)
    86,910
     
      Has Code

    Generally you should pass a buffer and it's size to the native code which would then fill the buffer with string data or return error if buffer size it too small:

     

    extern "C"
    {
    	__declspec(dllexport) int passStr(WCHAR s, DWORD * length)
    	{
    		// Check if length is enough, return lengh needed and error code of not.
                    // Or copy string into the buffer, return no error and actual string length.
                    // Never return pointers to buffers allocated on the stack (and static buffers).
    	}
    }

     

    Also keep in mind Windows CE is Unicode based so all strings should use 16 bit characters.

    If you have an API which returns string as return parameters and you can not change that API then declare it as IntPtr and use Marshal (or Encoding) class to get actual string.


    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Wednesday, September 28, 2011 2:43 AM
    Avatar of Elvinj
    Elvinj
    Avatar of Elvinj

    Elvinj

    0
    Recent Achievements 2 0 0
    First Marked Answer First Forums Reply
    0
     
     

    Hi, may i know how to pass the the string back to c# from dll? I'm new to programming..

    eg, in dll, i want to pass "1234" to C#

    and one more question is how to assign a string stored in a variable to WCHAR s? eg

    string str = "1234";

    how to assign s = str ?

    thanks.

  • Wednesday, September 28, 2011 3:22 AM
    Avatar of Elvinj
    Elvinj
    Avatar of Elvinj

    Elvinj

    0
    Recent Achievements 2 0 0
    First Marked Answer First Forums Reply
    0
     
      Has Code

    Hi, I'm implementing the code as:

    C++ dll

    extern "C"
    {
    	__declspec(dllexport) int passStr(WCHAR *s, DWORD length)
    	{		
    		// Check if length is enough, return lengh needed and error code of not.
            // Or copy string into the buffer, return no error and actual string length.
            // Never return pointers to buffers allocated on the stack (and static buffers).
    		
    		char* str = "I'm from c++ dll";
    		int slen = strlen(str);
    		int i;
    
    		for(i = 0; i<slen; i++)
    		{
    			s[i] = (int)str[i];
    		}
    		s[slen] = 0;
    
    		
    		return 0;
    	}
    }
    


    C# caller

    [DllImport("WMDLL.dll")]
    public static extern int passStr(System.String s, int length);
    
    string plain = "I'm from c#                                     ";
    Win321.passStr(plain, plain.Length);
    MessageBox.Show("" + plain);
    

    Seen it is working..

  • Wednesday, September 28, 2011 3:28 AM
    Avatar of Ilya Tumanov
    Ilya Tumanov
    Avatar of Ilya Tumanov

    Ilya Tumanov

    MSFT

    86,910
    Recent Achievements 11 5 3
    Forums Replies VI First Helpful Vote Proposed Answerer I
    (MSFT)
    86,910
     
      Answer
    Look into standard C library for working with strings as well as into secure versions (strcpy(),  wcscpy_s(), etc.)
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, September 29, 2011 4:15 PM
    Avatar of ctacke
    ctacke
    Avatar of ctacke

    ctacke

    OpenNETCF Consulting

    MVP

    1,270
    Recent Achievements 9 0 0
    Forums Replies II First Forums Spam Report Forums Answerer II
    OpenNETCF Consulting
    (MVP)
    1,270
     
      Answer
    As Ilya points out, you should not return a string from a C function.  Here's a bit more of a description as to why.  It's important to understand the "why" of this.

Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.

Would you like to participate?

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值