Return char* From DLL

S

Samant.Trupti

Hi All,

I am facing a problem can anyone help.

I have a function in DLL
void getString(char *string)
{
strcpy(string, "My String);
printf("In Dll %s", string);
}

I am calling this function in my win32 consol app

char mystr[50];
strcpy(mystr, "Test");
getString(myStr);
printf("in main %s", mystr);

The result I am getting is:
In Dll My String
In Main Test

Why DLL is unable to retutn value? If I use this function in main it
is working fine. DLL not working.
Can anyone please explain?
Thanks
Trupti
 
G

GArlington

Hi All,

I am facing a problem can anyone help.

I have a function in DLL
void getString(char *string)
{
strcpy(string, "My String);
printf("In Dll %s", string);

}

I am calling this function in my win32 consol app

char mystr[50];
strcpy(mystr, "Test");
getString(myStr);
printf("in main %s", mystr);

The result I am getting is:
In Dll My String
In Main Test

Why DLL is unable to retutn value?

Your DLL (function) is NOT returning a value [void getString(char
*string)], you are attempting to modify the char* pointer.
AFAIR it used to work, try to print out the address of those strings
both in DLL's printf() and main's printf().
 
G

GArlington

Hi All,

I am facing a problem can anyone help.

I have a function in DLL
void getString(char *string)
{
strcpy(string, "My String);
printf("In Dll %s", string);

}

I am calling this function in my win32 consol app

char mystr[50];
strcpy(mystr, "Test");
getString(myStr);
printf("in main %s", mystr);

The result I am getting is:
In Dll My String
In Main Test

Why DLL is unable to retutn value? If I use this function in main it
is working fine. DLL not working.
Can anyone please explain?
Thanks
Trupti

If you want to return a value try
char* getString(char *string){
....
return yourString;
}
P.S. You will have to allocate memory to that string inside your DLL
function...
 
J

Joe Greer

Hi All,

I am facing a problem can anyone help.

I have a function in DLL
void getString(char *string)
{
strcpy(string, "My String);
printf("In Dll %s", string);
}

I am calling this function in my win32 consol app

char mystr[50];
strcpy(mystr, "Test");
getString(myStr);
printf("in main %s", mystr);

The result I am getting is:
In Dll My String
In Main Test

Why DLL is unable to retutn value? If I use this function in main it
is working fine. DLL not working.
Can anyone please explain?
Thanks
Trupti

I can say that I have done things like that without trouble. Off hand,
I would say to check your compiler options to make sure that your dll is
compiled the same way your console app is. Otherwise, I am left with
the thought that the posted code doesn't match exactly what is in your
actual code. I really think that there is some compiler option that is
different and causing the issue.

joe
 
F

fred.l.kleinschmidt

Hi All,

 I am facing a problem can anyone help.

I have a function in DLL
void getString(char *string)
{
   strcpy(string, "My String);
  printf("In Dll %s", string);

}

I am calling this function in my win32 consol app

char mystr[50];
strcpy(mystr, "Test");
getString(myStr);

myStr has not been defined
printf("in main %s", mystr);

mystr is not the same entity as myStr
The result I am getting is:
In Dll My String
In Main Test

Why DLL is unable to retutn value?  If I use this function in main it
is working fine.  DLL not working.
Can anyone please explain?

Cut and paste the exact code that you think is a problem.
The above snippets will not compile.
 
J

James Connell

I am calling this function in my win32 consol app
Why DLL is unable to retutn value? If I use this function in main it
is working fine. DLL not working.
Can anyone please explain?

I have this problem with MS VC8 and up. That compiler refuses to allow
char * to be returned from a DLL ( although it will return other pointer
values ). This problem does not occur in any other compiler I have, nor
in versions of MSVC below V8.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top