Search code examples
c++dllstrncpysilktest

DLL call to a method involving strncpy()


I have created a DLL with following methods:

typedef struct names {
  const char* name1;
  const char* name2;
} rNames;

typedef struct useNames {
   rNames fullName;
} rUseNames;

rUseNames finalName;

int set_name() {
  finalName.fullName.name1 = "Alfred";
  finalName.fullName.name2 = "Allen";
  return 1;
}

int __stdcall get_name(char* first, char* last) {
  strncpy((char*)first, finalName.fullName.name1, 
     strlen(finalName.fullName.name1));
  strncpy((char*)last, finalName.fullName.name2, 
     strlen(finalName.fullName.name2));
  return 1;
}

I am calling get_name via SilkTest as below:

ansicall int get_name(out string fname, out string lname); //dll method 
//declaration

STRING name = SPACE(256) //This initialises the name to 256 blank characters
STRING lname = SPACE(256)
get_name(name, lname)

print(name)
print(lname)

The Problem:

I am getting a blank/empty string as output and NOT "Alfred Allen". Ideally, name should be filled with the content Alfred and lname with Allen.

NOTE: Consider that set_name() is being called internally and the name is already set before the dll call is made.


Solution

  • Are you sure finalName.fullName.name1 and finalName.fullName.name2 are getting set? Please ensure that. To null terminate you can do first[strlen(finalName.fullName.name1)] = '\0';