Access Violation When Function Reaches END; lazarus / 21-General ---------------------------------------------------------------------- Weitentaaal · 2021-03-15 07:01 Hello Guys My Problem: I Call a External DLL and already had many Problems with String Conversations. Now i Got this AV but it doesn't tell me much where the Error is. Here my Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  //DefTZAJsonRequestBSTR = Function(sRequest : WideString): WideString; stdcall; //Calling The DLLFunction Call_ZAJsonRequestBSTR(sRequest : WideString): WideString;var   // Legt eine passende Variable (Datenfeld) für das DLL-Unterprogramm an   ZAJsonRequestBSTR : TZAJsonRequestBSTR;   // Legt einen Handle für den Handle der DLL an   LibHandle: THandle;begin   //Einbinden Der DLL   LibHandle := LoadLibrary(PChar('C:\DLLS\FANselect.dll'));   if(LibHandle <> 0)then begin      // Weisst der Variablen ZAJsonRequestBSTR die Adresse des Unterprogrammaufrufs zu      // 'ZAJsonRequestBSTR' aus der DLL FANSelect.dll zu.      Pointer(ZAJsonRequestBSTR) := GetProcAddress(LibHandle, 'ZAJsonRequestBSTR');       // Prüft, ob eine gültige Adresse zurück gegeben wurde      if @ZAJsonRequestBSTR <> nil then begin         //Blackbox aufrufen         result := ZAJsonRequestBSTR(sRequest);    End;   End;    // Freigabe des Arbeitsspeichers   ZAJsonRequestBSTR := nil;   FreeLibrary(LibHandle);end;  //Calling in Code//Both Strings Here are WideStrings (because WideString Supports COM Memory of BSTR)JZaRueckgabe := DLLAufrufe.Call_ZAJsonRequestBSTR(JZa);      Hope u can help me. ---------------------------------------------------------------------- Zvoni · 2021-03-15 07:22 WideString vs. PWideString? Without seeing the Function-Prototype of the dll-function it's gazing into a crystal ball ---------------------------------------------------------------------- Weitentaaal · 2021-03-15 07:27 There is the Problem .. i dont't Have the DLL Code  Tested With PWideString but got the Same Error. The Calling Worked Fine in VB6, checked if i Have the Same input, wich i had. But no Output Somehow. But Still why do i get Av when the Code reaches End; ? is it because of an empty String wich returns ? ---------------------------------------------------------------------- Mr.Madguy · 2021-03-15 08:36 Please provide initial VB6 declaration of this function. I guess, problem is caused by simple fact, that string is allocated by DLL's memory manager and FPC fails to deallocate it properly. BSTR is OLE string, allocated by SysAllocString. May be it should be represented as OleVariant? Is your application 32bit? Because VB6 is 32bit and therefore DLL should be 32bit too. ---------------------------------------------------------------------- Weitentaaal · 2021-03-15 09:03 VB6 Code : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i            //Definition          Public Declare Function ZAJsonRequestBSTR Lib "C:\WGKBlck\Ziehl-Abegg\FANselect.dll" (ByVal sRequest As String) As String       //Called like that      JZa = StrConv(JZa, vbUnicode)    JZa2 = StrConv(JZa2, vbUnicode)          JZaRückgabe = StrConv(ZAJsonRequestBSTR(JZa), vbFromUnicode)  Didn't Tested with OLE variant till now. Application is 32 bit ---------------------------------------------------------------------- Zvoni · 2021-03-15 11:42 IIRC, in vb6 passing a String ByVal to an external dll is still passing the address to the String. And a Returntype of string is probably still a pointer to the Resultstring That's why i figured PWideString. Have you tried both as PWideString? the Argument and the return-Type? ---------------------------------------------------------------------- PascalDragon · 2021-03-15 12:04 Quote from: Weitentaaal on March 15, 2021, 08:01:22 amHere my Code: Two remarks (though both are probably not related to your problem): - you need to use FreeLibrary only if LibHandle is not 0 - to check whether the function was loaded correctly you need to use ZAJsonRequestBSTR <> Nil (or Assigned(ZAJsonRequestBSTR)); using @ZAJsonRequestBSTR will check whether the variable itself has a valid address. Quote from: Zvoni on March 15, 2021, 12:42:27 pmIIRC, in vb6 passing a String ByVal to an external dll is still passing the address to the String. And a Returntype of string is probably still a pointer to the Resultstring That's why i figured PWideString. Have you tried both as PWideString? the Argument and the return-Type? According to the manual (page 42) of the library the C(++) declaration for the functions is as follows: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i external “C” {  __declspec(dllimport)  const char* __stdcall ZAJsonRequestA(const char *szReq);   __declspec(dllimport)  const wchar_t* __stdcall ZAJsonRequestW(const wchar_t *szReq);   __declspec(dllimport)  const BSTR __stdcall ZAJsonRequestBSTR(BSTR sReq);} So using WideString instead of PWideString is indeed correct. ---------------------------------------------------------------------- Weitentaaal · 2021-03-15 12:15 Not Sure what i should do next but i bet the Problem is there because of the type. The Function did the Job it had to in vb6 so there must be the Problem in the in and output, but i don't get it. WideString Should be Supported by BSTR or am i wrong ? So where else could this Call go wrong ? I Will Test if its Working with pointers... Thanks for ur Replys, i realy appreciate. ---------------------------------------------------------------------- Zvoni · 2021-03-15 13:19 Quote from: PascalDragon on March 15, 2021, 01:04:34 pmQuote from: Weitentaaal on March 15, 2021, 08:01:22 amHere my Code: Two remarks (though both are probably not related to your problem): - you need to use FreeLibrary only if LibHandle is not 0 - to check whether the function was loaded correctly you need to use ZAJsonRequestBSTR <> Nil (or Assigned(ZAJsonRequestBSTR)); using @ZAJsonRequestBSTR will check whether the variable itself has a valid address. Quote from: Zvoni on March 15, 2021, 12:42:27 pmIIRC, in vb6 passing a String ByVal to an external dll is still passing the address to the String. And a Returntype of string is probably still a pointer to the Resultstring That's why i figured PWideString. Have you tried both as PWideString? the Argument and the return-Type? According to the manual (page 42) of the library the C(++) declaration for the functions is as follows: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i external “C” {  __declspec(dllimport)  const char* __stdcall ZAJsonRequestA(const char *szReq);   __declspec(dllimport)  const wchar_t* __stdcall ZAJsonRequestW(const wchar_t *szReq);   __declspec(dllimport)  const BSTR __stdcall ZAJsonRequestBSTR(BSTR sReq);} So using WideString instead of PWideString is indeed correct. Wait a sec. QuoteA BSTR is a composite data type that consists of a length prefix, a data string, and a terminator. The following table describes these components. Remarks Item    Description Length prefix    A four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminator. Data string    A string of Unicode characters. May contain multiple embedded null characters. Terminator    A NULL (0x0000) WCHAR.How does WideString handle that? @Weitentaaal: Have you tried the ZAJsonRequestW-Function? ---------------------------------------------------------------------- Mr.Madguy · 2021-03-15 13:35 Quote from: Zvoni on March 15, 2021, 02:19:59 pmHow does WideString handle that? @Weitentaaal: Have you tried the ZAJsonRequestW-Function? Manual says, that WideString is right type for BSTR. But using ZAJsonRequestW is better idea. I would also use asm debugger to find exact cause of this problem. ---------------------------------------------------------------------- lucamar · 2021-03-15 13:56 Quote from: Zvoni on March 15, 2021, 02:19:59 pmWait a sec. QuoteA BSTR is a composite data type that consists of a length prefix, a data string, and a terminator. The following table describes these components. Remarks Item    Description Length prefix    A four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminator. Data string    A string of Unicode characters. May contain multiple embedded null characters. Terminator    A NULL (0x0000) WCHAR.How does WideString handle that? Exactly like that; see the Programmer's Guide,  Memory Issues: WideStrings. It can't be otherwise since its main use is for COM interfaces. ---------------------------------------------------------------------- Weitentaaal · 2021-03-15 16:17 Finished work for Today but i will try it Tomorrow. Thank u all Guys for ur patience with me . ---------------------------------------------------------------------- engkin · 2021-03-15 16:39 Quote from: lucamar on March 15, 2021, 02:56:01 pmQuote from: Zvoni on March 15, 2021, 02:19:59 pmWait a sec. QuoteA BSTR is a composite data type that consists of a length prefix, a data string, and a terminator. The following table describes these components. Remarks Item    Description Length prefix    A four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminator. Data string    A string of Unicode characters. May contain multiple embedded null characters. Terminator    A NULL (0x0000) WCHAR.How does WideString handle that? Exactly like that; see the Programmer's Guide,  Memory Issues: WideStrings. It can't be otherwise since its main use is for COM interfaces. I see a difference, the length is in negative location offset. Am I right? Edit: If am right, the dll function is taking the first four byes of the WideString, that is the first two WideChars, as the wrong length of the string. For a string like: 'ABC', this would seem to the dll function as a string that is $00420041 bytes long, while in fact it is only 6 bytes long plus two terminating bytes. ---------------------------------------------------------------------- Mr.Madguy · 2021-03-15 17:23 Quote from: engkin on March 15, 2021, 05:39:08 pmI see a difference, the length is in negative location offset. Am I right? Edit: If am right, the dll function is taking the first four byes of the WideString, that is the first two WideChars, as the wrong length of the string. For a string like: 'ABC', this would seem to the dll function as a string that is $00420041 bytes long, while in fact it is only 6 bytes long plus two terminating bytes. Managed strings work exactly this way in Pascal - they have explicit length field and trailing null. ---------------------------------------------------------------------- engkin · 2021-03-15 17:45 Quote from: Mr.Madguy on March 15, 2021, 06:23:30 pmQuote from: engkin on March 15, 2021, 05:39:08 pmI see a difference, the length is in negative location offset. Am I right? Edit: If am right, the dll function is taking the first four byes of the WideString, that is the first two WideChars, as the wrong length of the string. For a string like: 'ABC', this would seem to the dll function as a string that is $00420041 bytes long, while in fact it is only 6 bytes long plus two terminating bytes. Managed strings work exactly this way in Pascal - they have explicit length field and trailing null. Not exactly,. The offset is negative, AND the value of length is the number of (Wide)Chars, instead of bytes. For WideString, that is 3 instead of 6 in the 'ABC' example. WideString in not equivalent to BStr. Edit: Both, WideString and BStr, use negative offset for length. But the value is not the same. ---------------------------------------------------------------------- Remy Lebeau · 2021-03-15 18:08 Quote from: PascalDragon on March 15, 2021, 01:04:34 pmAccording to the manual (page 42) of the library the C(++) declaration for the functions is as follows: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i external “C” {  __declspec(dllimport)  const char* __stdcall ZAJsonRequestA(const char *szReq);   __declspec(dllimport)  const wchar_t* __stdcall ZAJsonRequestW(const wchar_t *szReq);   __declspec(dllimport)  const BSTR __stdcall ZAJsonRequestBSTR(BSTR sReq);} So using WideString instead of PWideString is indeed correct. Even though WideString is the correct wrapper to use for a BSTR string, there is an interop issue to be aware of.  I don't know if FreePascal suffers from this issue or not, but in Delphi a function that has a WideString return value is NOT interop-compatible with a C/C++ function that returns a BSTR pointer!  On the C/C++ side, the function will return the BSTR pointer in the EAX cpu register, but on the Delphi side the BSTR will be expected to be received in a hidden var output parameter instead.  See: Why can a WideString not be used as a function return value for interop?. In this case, the solution on the Pascal side would be to declare ZAJsonRequestBSTR() as returning a PWideChar/BSTR instead, and then cast it to WideString after the function exits, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //DefTZAJsonRequestBSTR = function(const sRequest : WideString): BStr; stdcall; //Calling The DLLfunction Call_ZAJsonRequestBSTR(sRequest : WideString): WideString;var  // Legt eine passende Variable (Datenfeld) für das DLL-Unterprogramm an  ZAJsonRequestBSTR : TZAJsonRequestBSTR;  // Legt einen Handle für den Handle der DLL an  LibHandle: THandle;begin  Result := '';   //Einbinden Der DLL  LibHandle := LoadLibrary(PChar('C:\DLLS\FANselect.dll'));  if (LibHandle <> 0) then  try    // Weisst der Variablen ZAJsonRequestBSTR die Adresse des Unterprogrammaufrufs zu    // 'ZAJsonRequestBSTR' aus der DLL FANSelect.dll zu.    Pointer(ZAJsonRequestBSTR) := GetProcAddress(LibHandle, 'ZAJsonRequestBSTR');     // Prüft, ob eine gültige Adresse zurück gegeben wurde    if @ZAJsonRequestBSTR <> nil then begin      //Blackbox aufrufen      Pointer(Result) := ZAJsonRequestBSTR(sRequest);    end;  finally    FreeLibrary(LibHandle);  end;end;  //Calling in Code//Both Strings Here are WideStrings (because WideString Supports COM Memory of BSTR)JZaRueckgabe := DLLAufrufe.Call_ZAJsonRequestBSTR(JZa); Or, simply don't use ZAJsonRequestBSTR() at all, use ZAJsonRequestA() or ZAJsonRequestW() instead, as Mr.Madguy and Zvoni suggested.  They don't suffer from this interop issue. ---------------------------------------------------------------------- Remy Lebeau · 2021-03-15 18:09 Quote from: engkin on March 15, 2021, 06:45:50 pmWideString in not equivalent to BStr. WideString wraps a BStr internally. ---------------------------------------------------------------------- engkin · 2021-03-15 18:22 Quote from: Remy Lebeau on March 15, 2021, 07:09:52 pmQuote from: engkin on March 15, 2021, 06:45:50 pmWideString in not equivalent to BStr. WideString wraps a BStr internally. They use different values for length, WideChars for WideString vs bytes for BStr. That is x2 factor. Using ZAJsonRequestW is the way to go. Edit: As a consequence, a call like this in your post above: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    Pointer(Result) := ZAJsonRequestBSTR(sRequest);Will make the dll see half of sRequest, and the Result will have double the real result in length. ---------------------------------------------------------------------- engkin · 2021-03-15 19:06 I was deceived by the documentation. The difference between WideString and UnicodeString is not just reference counting, WideString uses the length in bytes just like BStr, while UnicodeString uses WideChar counts ---------------------------------------------------------------------- Remy Lebeau · 2021-03-15 19:57 Quote from: engkin on March 15, 2021, 07:22:45 pmThey use different values for length, WideChars for WideString vs bytes for BStr. That is x2 factor. You are thinking of UnicodeString, not WideString. On Windows, WideString is literally just a COM BSTR (FreePascal's documentation even says so!), with compiler magic to automatically call things like SysAllocString(), SysFreeString(), etc on it for you.  Always has been (in both FreePascal and Delphi).  The actual character data is stored in the BSTR only, and querying a WideString for its Length() is just a delegated call to SysStringLen(), which returns the number of characters, though the BSTR itself stores the length as bytes.  So no, the length values between WideString and BSTR are NOT different.  If that were the case, then WideString COULD NOT be used in place of BSTR in COM code at all, but it can, because they are literally the same thing. On non-Windows, WideString is just an alias for UnicodeString. Quote from: engkin on March 15, 2021, 07:22:45 pmUsing ZAJsonRequestW is the way to go. Agreed.  BSTR/WideString should never be used outside of COM code, unless you are using an older FreePascal/Delphi version that does not support UnicodeString.  Otherwise, use UnicodeString for non-COM Unicode work. ---------------------------------------------------------------------- engkin · 2021-03-15 20:41 Quote from: Remy Lebeau on March 15, 2021, 08:57:46 pmQuote from: engkin on March 15, 2021, 07:22:45 pmThey use different values for length, WideChars for WideString vs bytes for BStr. That is x2 factor. You are thinking of UnicodeString, not WideString. Yes, I realized my mistake and tried to slash my wrong comments out. Here is where the documentation went wrong(!): QuoteOn Windows platforms, a widestring is allocated on the heap, much like a unicode string, and is terminated with a double null. In difference with a unicode string, it is not reference counted. It says nothing about the length being in bytes, unlike UnicodeString. Also, UnicodeString uses 8 bytes for length on 64-bit systems, while WideString uses 4 bytes, at least on Windows. ---------------------------------------------------------------------- Remy Lebeau · 2021-03-15 21:02 Quote from: engkin on March 15, 2021, 09:41:39 pmHere is where the documentation went wrong(!): https://wiki.freepascal.org/FPC_Unicode_support QuoteWidestring is the same as a "COM BSTR" on Windows, and an alias for UnicodeString on all other platforms. Its string data is encoded using UTF-16. ---------------------------------------------------------------------- engkin · 2021-03-15 21:50 Quote from: Remy Lebeau on March 15, 2021, 10:02:59 pmQuote from: engkin on March 15, 2021, 09:41:39 pmHere is where the documentation went wrong(!): https://wiki.freepascal.org/FPC_Unicode_support QuoteWidestring is the same as a "COM BSTR" on Windows, and an alias for UnicodeString on all other platforms. Its string data is encoded using UTF-16. Yes, the wiki does make it clear, unlike the aforementioned documentation page. ---------------------------------------------------------------------- Mr.Madguy · 2021-03-16 08:30 That's why knowing asm is so important. I can't say what is reason for this problem without asm debugging. Please set breakpoint on last line, that works fine. Then use "View->Debug windows->Assembler" and "View->Debug windows->Registers". Use one of two buttons at the top of assembler window to "Step over". Yeah, it's not Turbo Debugger, used in Delphi, that allows you to simply press F7/F8. Make screenshot of instruction, that causes access violation. But please note! Use Intel assembler style! Newer versions of Lazarus should provide option to choose it somewhere. I'm not used to AT&T one. ---------------------------------------------------------------------- Weitentaaal · 2021-03-16 11:40 Used ZAJsonRequestW but got The Same AV. (Just Replaced ZAJsonRequestBSTR with ZAJsonRequestW but not the input and output Strings (WideString)) Quote from: Mr.Madguy on March 16, 2021, 09:30:34 amThat's why knowing asm is so important. I can't say what is reason for this problem without asm debugging. Please set breakpoint on last line, that works fine. Then use "View->Debug windows->Assembler" and "View->Debug windows->Registers". Use one of two buttons at the top of assembler window to "Step over". Yeah, it's not Turbo Debugger, used in Delphi, that allows you to simply press F7/F8. Make screenshot of instruction, that causes access violation. But please note! Use Intel assembler style! Newer versions of Lazarus should provide option to choose it somewhere. I'm not used to AT&T one. Did it (Hope so atleast ) ... see Attachment Edit1 : And Sorry bu did't knew exactly what u ment by Using Intel Assembler Style and where to set it. If i can find out i will update this Reply Edit2 : Found it but assembler Still looks the Same.. even after Restarting IDE Edit3: Attached 1 More Screenshot, maybe this 1 helps a bit ---------------------------------------------------------------------- Mr.Madguy · 2021-03-16 13:09 Sorry, I was busy today. Unfortunately this screenshots aren't informative, because it's not place, where access violation happens - it's implicit exception handler, that frees manged resources in case of exception inside procedure. Can you specify instruction, that causes jump to that "Reraise" block? It just can't be "lea eax, [ebp-4]". May be next one? That "fpc_widestr_dec_ref"? I wanted to ask you to do another thing, but didn't have time to do it. I would be better, if you would record video. Set breakpoint on ZAJsonRequestW and record video about everything, that happens till access violation. Make sure, that register values are fully visible. First thing, we need to take care about - if stack returns to proper state after ZAJsonRequestW call. If it doesn't, then may be result is returned as variable, not in register. P.S. I guess, in case of ZAJsonRequestW you should use PWideChar, instead of WideString. ---------------------------------------------------------------------- Weitentaaal · 2021-03-16 13:32 This is getting realy complex right now... isn't there Somewhere an Example to how to use ZAJsonRequestW ? If there is ot i guess i have to record that Video. But Thank u So much man ---------------------------------------------------------------------- Zvoni · 2021-03-16 14:01 I'm pretty sure with the "-W"-Function you have to use Pointers (Which one? PChar? PWideChar?) for the Func-Argument as well as for the Return-Type EDIT: Just saw MadGuy saying the same in his P.S. ---------------------------------------------------------------------- Mr.Madguy · 2021-03-16 14:31 Quote from: Weitentaaal on March 16, 2021, 02:32:38 pmThis is getting realy complex right now... isn't there Somewhere an Example to how to use ZAJsonRequestW ? If there is ot i guess i have to record that Video. But Thank u So much man It should be "function ZAJsonRequestW(S:PWideChar):PWideChar;". May be even "const S:PWideChar". Because FPC will convent string to PWideChar and without "const" it may try to convert it back. I also had problems with conversion from UTF-8 string to PWideChar. It doesn't work automatically. For example for CreateProcessW I used something like this "PWideChar(UTF8Decode(CurrDir)),". And in order to get proper result, you should use UTF8Encode. I.e. for example CreatePorcessW turns into this in pascal: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CreateProcessW(lpApplicationName: LPWSTR; lpCommandLine: LPWSTR; lpProcessAttributes, lpThreadAttributes: PSecurityAttributes; bInheritHandles: BOOL; dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPWSTR;  const lpStartupInfo: TStartupInfoW; var lpProcessInformation: TProcessInformation): BOOL; external 'kernel32' name 'CreateProcessW'; and LPWSTR is Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LPWSTR = Pwidechar;  And of course it's better to see debugging process in dynamic. First thing to notice - esp register value before pushing parameters to stack and after function call. It should be the same. If it changes - then number of parameters or their types are wrong. ---------------------------------------------------------------------- Weitentaaal · 2021-03-16 15:12 Thanks Guys It Did work with The Pointers. Not The Result i wanted but atleast i dont get AV Anymore. Thanks again for ur effort and patience. ---------------------------------------------------------------------- Remy Lebeau · 2021-03-16 16:28 Quote from: Weitentaaal on March 16, 2021, 02:32:38 pmisn't there Somewhere an Example to how to use ZAJsonRequestW ? Try this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //DefTZAJsonRequestW = function(sRequest : PWideChar): PWideChar; stdcall; //Calling The DLLFunction Call_ZAJsonRequestW(const sRequest : UnicodeString): UnicodeString;var  // Legt eine passende Variable (Datenfeld) für das DLL-Unterprogramm an  ZAJsonRequestW : TZAJsonRequestW;  // Legt einen Handle für den Handle der DLL an  LibHandle: THandle;begin  Result := '';  //Einbinden Der DLL  LibHandle := LoadLibrary(PChar('C:\DLLS\FANselect.dll'));  if (LibHandle <> 0) then  try    // Weisst der Variablen ZAJsonRequestW die Adresse des Unterprogrammaufrufs zu    // 'ZAJsonRequestW' aus der DLL FANSelect.dll zu.    Pointer(ZAJsonRequestW) := GetProcAddress(LibHandle, 'ZAJsonRequestW');     // Prüft, ob eine gültige Adresse zurück gegeben wurde    if Assigned(ZAJsonRequestW) then begin      //Blackbox aufrufen      Result := ZAJsonRequestW(PWideChar(sRequest));    end;  finally    FreeLibrary(LibHandle);  end;end; //Calling in Code//Both Strings Here are UnicodeStringsJZaRueckgabe := DLLAufrufe.Call_ZAJsonRequestW(JZa); Alternatively: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //DefTZAJsonRequestW = function(sRequest : PWideChar): PWideChar; stdcall; //Calling The DLLFunction Call_ZAJsonRequestW(const sRequest : UTF8String): UTF8String;var  // Legt eine passende Variable (Datenfeld) für das DLL-Unterprogramm an  ZAJsonRequestW : TZAJsonRequestW;  // Legt einen Handle für den Handle der DLL an  LibHandle: THandle;begin  Result := '';  //Einbinden Der DLL  LibHandle := LoadLibrary(PChar('C:\DLLS\FANselect.dll'));  if (LibHandle <> 0) then  try    // Weisst der Variablen ZAJsonRequestW die Adresse des Unterprogrammaufrufs zu    // 'ZAJsonRequestW' aus der DLL FANSelect.dll zu.    Pointer(ZAJsonRequestW) := GetProcAddress(LibHandle, 'ZAJsonRequestW');     // Prüft, ob eine gültige Adresse zurück gegeben wurde    if Assigned(ZAJsonRequestW) then begin      //Blackbox aufrufen      Result := UTF8Encode(ZAJsonRequestW(PWideChar(UTF8Decode(sRequest))));    end;  finally    FreeLibrary(LibHandle);  end;end; //Calling in Code//Both Strings Here are UTF8StringsJZaRueckgabe := DLLAufrufe.Call_ZAJsonRequestW(JZa); ---------------------------------------------------------------------- Mr.Madguy · 2021-03-16 18:53 Quote from: Weitentaaal on March 16, 2021, 04:12:38 pmThanks Guys It Did work with The Pointers. Not The Result i wanted but atleast i dont get AV Anymore. Thanks again for ur effort and patience. BSTR variant need extensive testing with asm debugger. But I don't have time to do it and don't want to mess with some libraries, I'm unfamiliar with. Sometimes only asm debugging can fix problem. For example one of hardest tasks for me was - to use 7z.so in my Lazarus project. It had taken so much time to realize, what kind of strings are used there. ---------------------------------------------------------------------- Weitentaaal · 2021-03-17 07:19 Quote from: Mr.Madguy on March 16, 2021, 07:53:21 pmQuote from: Weitentaaal on March 16, 2021, 04:12:38 pmThanks Guys It Did work with The Pointers. Not The Result i wanted but atleast i dont get AV Anymore. Thanks again for ur effort and patience. BSTR variant need extensive testing with asm debugger. But I don't have time to do it and don't want to mess with some libraries, I'm unfamiliar with. Sometimes only asm debugging can fix problem. For example one of hardest tasks for me was - to use 7z.so in my Lazarus project. It had taken so much time to realize, what kind of strings are used there. 100 % Agree on that... we never looked that detailed into "Strings" in Shool (finished Shool last year)... first expreience with Strings like that.