passing string from c++ dll to c#
-
Tuesday, September 27, 2011 7:24 AM
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 AMIlya Tumanov
Ilya Tumanov
MSFT
Recent Achievements 11 5 3Forums Replies VI First Helpful Vote Proposed Answerer I(MSFT)86,910Look 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.- Proposed As Answer by Jesse JiangModerator Friday, September 30, 2011 7:20 AM
- Marked As Answer by Jesse JiangModerator Wednesday, October 05, 2011 8:26 AM
-
Thursday, September 29, 2011 4:15 PMctacke
ctacke
OpenNETCF Consulting
MVP
Recent Achievements 9 0 0Forums Replies II First Forums Spam Report Forums Answerer IIOpenNETCF Consulting(MVP)1,270As 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.- Marked As Answer by Jesse JiangModerator Wednesday, October 05, 2011 8:26 AM
All Replies
-
Tuesday, September 27, 2011 8:42 AMvinothmdu2001
vinothmdu2001
e con systems India Pvt Ltd
Recent Achievements 7 0 0First Answer Confirmed Forums Answerer I First Helpful Votee con systems India Pvt Ltd180Hi,
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 PMIlya Tumanov
Ilya Tumanov
MSFT
Recent Achievements 11 5 3Forums Replies VI First Helpful Vote Proposed Answerer I(MSFT)86,910Generally 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
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
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 AMIlya Tumanov
Ilya Tumanov
MSFT
Recent Achievements 11 5 3Forums Replies VI First Helpful Vote Proposed Answerer I(MSFT)86,910Look 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.- Proposed As Answer by Jesse JiangModerator Friday, September 30, 2011 7:20 AM
- Marked As Answer by Jesse JiangModerator Wednesday, October 05, 2011 8:26 AM
-
Thursday, September 29, 2011 4:15 PMctacke
ctacke
OpenNETCF Consulting
MVP
Recent Achievements 9 0 0Forums Replies II First Forums Spam Report Forums Answerer IIOpenNETCF Consulting(MVP)1,270As 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.- Marked As Answer by Jesse JiangModerator Wednesday, October 05, 2011 8:26 AM
|
My Forum Links
Related Topics
= Unanswered = Answered
- CALL C++ DLL from C# (passing a string back and forth)
- Issue Passing string from C# to C++ dll method and manipulating it.
- Help: Pass string from MFC(c++) to c#
- Passing string from C# to C++ dll and getting updated string back ...
- Passing a string from a C# program to a Win32 C++ dll?
- passing string from C# to C++
- Strings Interop - Passing Strings C# to C++
- Passing multi-dimensional array from C# to Win32 C++ dll?
- Passing A byte array to a c++ dll from c#
- pass C# string to C++ DLL
Statistics
- Started: 9/27/2011
- Last Reply: 9/29/2011
- Helpful Votes: 1
- Replies: 6
- Views: 1,882