I'm having a little trouble returning the current date from a dll function.

Here is my code

LPSTR __stdcall date()
	
     {
	  int i;
          char dateStr [10];
          _strdate( dateStr);
	  LPSTR string = "";
	  for (i = 0; i < 10; i++)
		 {
			 string += dateStr[i];
		 }
	  std::cout<<dateStr<<"\n";
          return dateStr;
     }

I cant get the return from it when called, but it will output to the console the correct date.

I'm hoping someone can help me understand why.
There are a number of other functions in the dll that all work fine.

(edit) the return from the call is blank.

Suze.

character arrays like that can not be returned by any function, whether its in a dll or not makes no difference. You have several options

  1. Since this is c++ return std::string -- this may have the same problem as described below for #3
  2. add a char array parameter to that function void __stdcall date(LPSTR dateStr, size_t size)
  3. declare dateStr as a pointer and allocate memory for it. This is least desireable because the calling function has no way to delete the memory. Memory allocated in a dll must be deleted (or free'ed) in the same dll.

>>LPSTR string = "";

That declares a pointer which points to a character array of only 1 byte -- the NULL terminator.

>>string += dateStr;
This will not work with character arrays. All that it is doing is incrementing the pointer by dateStr number of bytes. If dateStr == 'A' then pointer is incremented by 65 bytes because 'A' has a decimal value of 65. Its the same as this: string += 65; That is clearly not what you intended.

Lines 7-11 do not seem to have a purpose anyway, so just delete them.

I have fought with this for 10 hours and once again I'm so losing
the battle vs C++ char arrays and strings.

I have tried so much I've forgotten most and my head is all twisted up.

If I try to use anything from the std library I just have crash code.

for instance here I am now.

string __stdcall date()
	
     {
		 
          char dateStr[50]; // declare char array
	  _strdate( dateStr); // fills char array with date
	  string dateStr2; // declar std::string
	  stringstream ss; // create a stringstream 
	  ss << dateStr[0]; // put char array into stream
	  ss >> dateStr2; // put stream into string
	  cout<<dateStr<<"\n";// output char array (date) to console
          return dateStr2; // return string
     }

I was under the illusion that that stringstream converted data to string
but alas, just another crash code.

I admit I did not really understand most of the advice above, but I tried
to follow the best I could without success.

I'd really appreciate some help, preferably in a primary school fashion.

>>I have fought with this for 10 hours and once again I'm so losing
the battle vs C++ char arrays and strings.

Because you must be too pigheaded and stubborn to listen to the advice I gave you in my previous post. You just wasted 10 hours for what? Nothing. Had you bothered to read my post 15 hours ago you would have discovered that what you are attempting to do can not be done.

I already told you that character arrays can not be returned from functions. Why? because the array is destroyed as soon as the function returns.

>>ss << dateStr[0];
That doesn't work becase all it does is put the first chararacter of dateStr into ss. If you want to put the entire string then do this: ss << dateStr; . But why are you using stringstream here? The code you posted does nothing with it so you might as well just delete it.

no-one knows more than i, that the code does nothing except crash, and I did try ss << dateStr;
Amongst a ton of other things.

I can see you are annoyed that I coulnt get my head around your code, but could you please just clarify, so I dont waste more time on this.

I cannot convert a char[] array to a string type which can be returned by a function ?

Here is one solution, the same as used by many win32 api functions because it avoids the problems of allocating memory in DLLs.

void __stdcall date(char* dateStr, size_t size)
// dateStr is allocated by calling function
// size is the number of bytes allocted to dateStr
	
{
		 
    _strdate_s( dateStr, size); // fills char array with date
}

Or if you want the function to return something

char* __stdcall date(char* dateStr, size_t size)
// dateStr is allocated by calling function
// size is the number of bytes allocted to dateStr
	
{
		 
    _strdate_s( dateStr, size); // fills char array with date
    return dateStr;
}

Example calling function

int main()
{
   char dateStr[50];
   date(dateStr, sizeof(dateStr));
   cout << dateStr << '\n';
}
commented: Very helpful, exactly what this noob needed. +1

Thank you very much for you time and patience.
Your examples were a great help to me.

I am calling the function from a different scripting language, but your calling example
helped me with that too.

The dll code was exactly as is above, and for anyone interested this is the call from Autoit3.

$struct = "char var[50]"

$DllStruct = DllStructCreate($struct)

$size = DllStructGetSize($DllStruct)

DllStructSetData($DllStruct, 1, $size)

$dllopen = DllOpen("MyDLL.dll")

$call = DllCall($dllopen, "str", "date", "str*",$DllStruct, "int", $size)

If @error Then
	ConsoleWrite(@error & @LF)
	Exit
Else
	ConsoleWrite($call[0] & @LF)
EndIf

DllClose($dllopen)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.