I'm doing some C# code which is using DLLImport to call a function inside my C++ DLL:
[DllImport("my.dll", EntryPoint = "#16", CallingConvention = CallingConvention.StdCall)]
private static extern void sendstring(string s);
I call it like this in C#:
sendstring("Test1\\0test2\\0");
My C++ DLL needs to create a static const char XY[] = "Test1\\0test2\\0"; from this, since I need that for calling another DLLs function from inside my c++ DLL like this:
functiontootherdll(sizeof(s),(void*)s);
So my code in C++:
extern "C" {
void MyClass::sendstring( const char *s) {
functiontootherdll(sizeof(s),(void*)s);
}
The problem: It is working, if I define the thing manually inside my C++ DLL like this:
static const char Teststring[] = "Test1\0test2\0";
functiontootherdll(sizeof(Teststring),(void*)Teststring);
but it is not taking the const char *s when calling this from my C# file (it will report different errors from the called other dll). I would need to know how I can cast the const char *s to something like static const char s[] or such.
As you realize I have little clue about all this, so any help is very welcome!
Alright, I found out a way I think:
I modified my C++ to:
extern "C" {
void MyClass::sendstring( const char *s) {
int le = strlen(s);
char p[256];
strcpy(p,s);
char XY[sizeof(p) / sizeof(*p) + 1];
int o=0;
for (int i = 0; i<le;i++) {
if (p[i] == ';') {
XY[i] = '\0';
} else {
XY[i] = p[i];
}
o++;
}
XY[o] = '\0';
functiontootherdll(sizeof(XY),(void*)XY);
}
Afterwards the function call to
functiontootherdll(sizeof(XY),(void*)XY);
is working fine.
Pls note that I send from my C# code now a string like "Test1;test2;test3;...", trying with the \\\\0 as separator did not work out. My call with C# is:
sendstring("Test1;test2;test3");
I don't know if this is a smart solution, but at least it is one :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.