0

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.

Corleone
  • 25
  • 6
  • Your `strncpy` calls do not null-terminate the output, maybe this is a problem – M.M Nov 07 '17 at 09:33
  • Is there a way to null terminate 'first' and 'last' dynamically? – Corleone Nov 07 '17 at 09:41
  • @Corleone: Yes, you need to copy strlen+1 to copy the terminating null. Note that you are abusing strncpy - used like this, it is not doing anything other than memcpy. (Also strncpy is almost never the function you want - it doesn't necessarily null terminate, and will fill the buffer with null characters if the buffer is too big.) – Martin Bonner supports Monica Nov 07 '17 at 11:06
  • I did use strncpy and defined the size to copy as strlen+1. Still getting the same issue though. – Corleone Nov 07 '17 at 12:06

2 Answers2

0
int __stdcall get_name(char* first, char* last) {

ansicall int get_name(out string name); //dll method declaration

So the first has two parameters, the second has one. They clearly don't match. How do you expect to get anything useful here?

Also, your test doesn't appear to call set_name, so finalName will only hold null pointers.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

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';

Valgrind1691
  • 182
  • 1
  • 11
  • Agreed. Null termination isn't a problem here. Perhaps, the names are getting set and then being reset internally. I will debug around this. Thanks! – Corleone Nov 07 '17 at 12:08
  • u can mark this as correct if u found this helpful in solving your problem :) – Valgrind1691 Nov 07 '17 at 12:19