Corleone
Corleone

Reputation: 25

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.

Upvotes: 0

Views: 88

Answers (2)

Valgrind1691
Valgrind1691

Reputation: 300

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

Upvotes: 0

Sebastian Redl
Sebastian Redl

Reputation: 71969

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.

Upvotes: 0

Related Questions