Jump to content
Sign in to follow this  
Andreik

Strings defined in struct

Recommended Posts

Andreik

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

Share this post


Link to post
Share on other sites
Richard Robertson

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.

Share this post


Link to post
Share on other sites
Andreik

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 by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites
Richard Robertson

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.

Share this post


Link to post
Share on other sites
Andreik

If is not too much, can you give me an example?


When the words fail... music speaks

Share this post


Link to post
Share on other sites
Richard Robertson

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.