Recent

Author Topic: c++ library and strings  (Read 1032 times)

bigeno

  • Sr. Member
  • ****
  • Posts: 266
c++ library and strings
« on: September 25, 2023, 09:47:56 am »
I'm trying to build dll library on VS and made some test function, its working for integer result but not for chars. Can someone help me with strings / chars ?
I need simple example on VS side and lazarus side.
My code VS C++:
Code: Pascal  [Select][+][-]
  1. extern __declspec(dllexport) char* HelloWorld(int val);
  2.  
  3. char* HelloWorld(int val)
  4. {
  5.     char str4[] = "hello";
  6.     return str4;
  7. }

and lazarus:
Code: Pascal  [Select][+][-]
  1. TfunStringBack = function(val : integer) : PChar;  stdcall;
  2.  
  3.  LibHandle:=LoadLibrary('test-lib.dll');
  4.  Pointer(funStringBack) := GetProcAddress(LibHandle, 'HelloWorld');
  5.  if @funStringBack <> nil then begin
  6.     S:=PChar(funStringBack(2));
  7.     debugln(S);
  8.  end;
  9.  proc:=nil;
  10.  FreeLibrary(LibHandle);
  11.  

alpine

  • Hero Member
  • *****
  • Posts: 1298
Re: c++ library and strings
« Reply #1 on: September 25, 2023, 09:58:08 am »
As it is HelloWorld written, it returns a local auto variable (allocated on stack) as a result. It ceases to exist.
Move the str4 outside the function.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: c++ library and strings
« Reply #2 on: September 25, 2023, 10:00:20 am »
- Since it is a c library it should probably read cdecl (not stdcall)
- You return a pchar. You have the function declared as such as well so there would be no need for the pchar cast. If any use strpas function to convert to a (short)string.

For more information regarding alpine's answer see for example here and here
« Last Edit: September 25, 2023, 10:13:13 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1430
    • Lebeau Software
Re: c++ library and strings
« Reply #3 on: September 25, 2023, 10:57:18 pm »
Move the str4 outside the function.

Or make it static inside the function.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1430
    • Lebeau Software
Re: c++ library and strings
« Reply #4 on: September 25, 2023, 10:58:31 pm »
- Since it is a c library it should probably read cdecl (not stdcall)

Which also changes its exported name, unless you use a .DEF file to set the exported name explicitly.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: c++ library and strings
« Reply #5 on: September 28, 2023, 09:42:40 pm »
- Since it is a c library it should probably read cdecl (not stdcall)

That it's a C-library does not necessarily mean that it's cdecl as one can decorate the C function with __declspec(stdcall) as well. In the specific example it's indeed cdecl (except if set to another default in the project settings).

- Since it is a c library it should probably read cdecl (not stdcall)

Which also changes its exported name, unless you use a .DEF file to set the exported name explicitly.

For the example as is that is not relevant. The default calling convention in MSVC is cdecl, thus the mangled name will indeed be the same (on 64-bit Windows at least). And changing the calling convention of the function pointer on the Pascal side (which is what TRon suggested) will not influence the mangling in any way.

 

TinyPortal © 2005-2018