Andreik Posted July 19, 2011 Share Posted July 19, 2011 How should look like a struct in C that contains two strings and this function to work properly? typedef struct { char *FirstName; char *LastName; } MyStruct; extern "C" int __declspec(dllexport) TestFunc (MyStruct &StringPass) { StringPass.FirstName = "This is test#1."; StringPass.LastName = "This is test#2."; return 1; } And call from AutoIt: #include <Array.au3> $STRUCT = DllStructCreate("char[16];char[16]") $RESULT = DllCall("StructTest.dll","int:cdecl","TestFunc","ptr",DllStructGetPtr($STRUCT)) _ArrayDisplay($RESULT) MsgBox(0,"",DllStructGetData($STRUCT,1)) ;Return HAöXAö MsgBox(0,"",DllStructGetData($STRUCT,2)) Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Richard Robertson Posted July 19, 2011 Share Posted July 19, 2011 It's really better not to use arbitrarily sized strings. If you are going to pass strings as pointers, make sure you ask for a length so you don't accidentally created a buffer overflow. You should also change it to a pointer to struct and not a reference. Link to comment Share on other sites More sharing options...
Andreik Posted July 19, 2011 Author Share Posted July 19, 2011 (edited) Maybe the above example was not the best one about what I`m trying to do. But your answer was good enought I understood something from what you said me and it's ok I can pass the length of my text to function but how can I set the size to string from struct every time with the desired length. Real example of what I want to do is to pass two strings to JaroWinkler function, to process this strings and get the JW distance. If I use fixed size then I cannot change the content of strings in function with another string with other length. Error said: cannot convert char[19] to char[255] In theory I should not change Text1 and Text2 in the function but if I have to do it how should I do? typedef struct { char Text1[255]; char Text2[255]; double JaroWinkler; } JaroWinklerStruct; extern "C" int __declspec(dllexport) JaroWinkler (JaroWinklerStruct *JWStruct) { JWStruct->Text1 = "Something for text"; JWStruct->Text2 = "Something for text"; /* code to get JW distance */ JWStruct->JaroWinkler = 2.55; return 1; } PS: I changed from reference to pointer to struct. EDIT: I changed in this and work good and, I think, I cannot accidentally created a buffer overflow, am I right Richard? typedef struct { char Text1[255]; char Text2[255]; double JaroWinkler; } JaroWinklerStruct; extern "C" int __declspec(dllexport) JaroWinkler (JaroWinklerStruct *JWStruct) { /* code to get JW distance */ strcpy_s(JWStruct->Text1,"This is a test#1"); strcpy_s(JWStruct->Text2,"This is a test#2"); JWStruct->JaroWinkler = 2.55; return 1; } Edited July 19, 2011 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Richard Robertson Posted July 19, 2011 Share Posted July 19, 2011 Yes.. that code will prevent an overflow, but that wasn't what I meant. I meant a parameter in the function that says how much buffer space was allocated. Link to comment Share on other sites More sharing options...
Andreik Posted July 19, 2011 Author Share Posted July 19, 2011 If is not too much, can you give me an example? When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Richard Robertson Posted July 19, 2011 Share Posted July 19, 2011 void example(char *buffer, int length) { } And just don't write longer than "length - 1" characters because you need the null terminator at the end. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now