Home > Software engineering >  Dear brother, how static call from c DLL char * type returned by the function
Dear brother, how static call from c DLL char * type returned by the function

Time:11-10



Dear brother, a program in c + + call this function in c + + DLL

Extern "C" char * __stdcall GetSomething ()

{
Char * Name="WuHan";
Return the Name;
}
The code is as follows:

Char * vID=GetSomething ();

Results show that the DLL NAME value is normal, but returned to the c + + program is empty, where is the problem that how to solve? Thank you for your brother.

CodePudding user response:

GetSomething function change, GetSomething (cstrings STR);

CodePudding user response:

__stdcall to clean up the stack when the function exits, generally cannot use local variables to do return value

Try __cdecl

Or make a proposal to buffer and buffer and length as the pointer passed, it is not affected by calling code
Extern "C" char * __stdcall GetSomething (char * szBuffer, int iLen)
{
Strcpy_s (szBuffer iLen, "XXXX");
Return szBuffer;
}

CodePudding user response:

The
reference 1/f, to be continued _1006 response:
GetSomething function change, GetSomething (cstrings STR);


Cstrings seems to need a header file support, I this is in a DLL in the program

CodePudding user response:

refer to the second floor zgl7903 response:
__stdcall to clean up the stack when the function exits, generally cannot use local variables to do return value

Try __cdecl

Or make a proposal to buffer and buffer and length as the pointer passed, it is not affected by calling code
Extern "C" char * __stdcall GetSomething (char * szBuffer, int iLen)
{
Strcpy_s (szBuffer iLen, "XXXX");
Return szBuffer;
}


__cdecl return is still empty, I think it should be clear after stack can't find the original memory location value

Then I used the formula behind, prompt error 2 error LNK1120: one cannot resolve the external command


CodePudding user response:

Char * Name="WuHan";//in the stack
Return the Name;//the name be destroy pointer is invalid!!!

CodePudding user response:

Char * Name="WuHan";
Port of reason is because in the global and static variables, but how to be optimized became
Char Name []="WuHan";
Became a local variable,

CodePudding user response:

Extern "C" char * __stdcall GetSomething ()

{
The static char Name []="WuHan";
Return the Name;
}

CodePudding user response:

With STD: : string more convenient, do not use char *, change char * to STD: : string is ok,

CodePudding user response:

Don't return to a local variable pointer or reference,

CodePudding user response:

refer to the original poster rookies response:
dear brother, a program in c + + call this function in c + + DLL

Extern "C" char * __stdcall GetSomething ()

{
Char * Name="WuHan";
Return the Name;
}
The code is as follows:

Char * vID=GetSomething ();

Results show that the DLL NAME value is normal, but returned to the c + + program is empty, where is the problem that how to solve? Thank you for your brother,



Define global string
The static const char * version="something";


Const char * GetSomeThing (void)
{
Return the version;
}

CodePudding user response:

STD: : string if different way to compile DLL transmission process, there may be memory release collapse, suggest using
Pbufs GetSomething (char * and an int size)
{
Strncpy (pbufs, "data", STD: : min (strlen (" data "), the size));
}

CodePudding user response:

Char * Name="WuHan" distribution within the stack, function complete, namely after the return of its life cycle is over, then executes the stack is then released, so not to return to a local variable function pointer, the parameters of the local variable/function is in the stack,

You can new a space, the open space is on the heap, function complete, will not be released, the need to manually release can; The new, and then copy the data to your application for the memory of the

As for the new issue of DLL in memory release after, is another question

CodePudding user response:

The best is you pass a buffer to the DLL, the DLL interface in the fill the buffer
  • Related