Jump to content

Strings defined in struct


 Share

Recommended Posts

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

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...