Page 1 of 1

I can not return a string of a external DLL

Posted: Fri Jul 24, 2015 11:40 pm
by ivega718
Please if somebody can help me...I am try and I not know how execute a function that retunrn a string. I was try using
prototype's...with numeric values is working fine...but with string I not know how...All function execute and working fine
returning correct values...only have problem with return string.

My DLL is VegaEVAL.DLL and this is code for execute functions of DLL:

The message displayed in debug is: Invalid memory access. (write error at address xxxxx)

Code: Select all

Procedure UseVegaEval(filename$)
  xDLL = OpenLibrary(#PB_Any, filename$)
  If Not xDLL
    MessageRequester("Dll not can be load","Error loading VEGAEVAL.DLL",0)
  EndIf
  ProcedureReturn xDLL
EndProcedure




DLLX=UseVegaEval("VEGAEVAL.DLL")

Prototype.l ProtoSetMaxVariables(maxvar.l)
SetMaxVariables.ProtoSetMaxVariables=GetFunction(DLLX,"SetMaxVariables")

Prototype.l ProtoSetMaxLines(maxlines.l)
SetMaxLines.ProtoSetMaxLines=GetFunction(DLLX,"SetMaxLines")

Prototype.l ProtoSetValue(var.s, value.d, mode.l)
SetValue.ProtoSetValue=GetFunction(DLLX,"SetValue")

Prototype.d ProtoGetValue(var.s)
GetValue.ProtoGetValue=GetFunction(DLLX,"GetValue")

Prototype.l ProtoSetTextValue(var.s, value.s, mode.l)
SetTextValue.ProtoSetTextValue=GetFunction(DLLX,"SetTextValue")


Prototype.l ProtoExeBasic(code.s, handle.l,mode.l)
ExeBasic.ProtoExeBasic=GetFunction(DLLX,"ExeBasic")


Prototype.i ProtoGetTextValue(var.s)
GetTextValue.ProtoGetTextValue=GetFunction(DLLX, "GetTextValue")





SetMaxVariables (10000)  
SetMaxLines (10000)  

SetValue ("A#", 100.25, 1)
SetValue ("B#", 1.50, 1)
SetValue ("C#", 999.99, 1)

ExeBasic ("C#=((A#+B#+C#)*18.14)/2",0,1)

C.D=GetValue("C#")
MessageRequester("Result",StrD(C.D),0)


SetTextValue("VAR1$","Israel Vega Alvarez",1)
ExeBasic ("VAR2$=VAR1$",0,1)
varx.s="VAR2$"

;This is problem for execute. I was try with 2 ways

;VAR2X$=PeekS(CallFunction(DLLX,"GetTextValue", @varx.s))
VAR2X$=PeekS(GetTextValue(VARX))

MessageRequester("Value of Variable: ",VAR2X$,0)

result.d=GetValue("C#")

MessageRequester("",StrD(RESULT.d),0)
      
CloseLibrary(DLLX)
     


[*]

Re: I can not return a string of a external DLL

Posted: Sat Jul 25, 2015 12:14 am
by jack
perhaps it would help others if you told what compiler you used in creating the dll also a download link so they can test.
also helpful would be to know what kind of string is used in the dll.

Re: I can not return a string of a external DLL

Posted: Sat Jul 25, 2015 12:19 am
by ivega718
I am using PowerBasic Compiler for make DLL...but need use from PureBasic.

my DLL can be download from:

http://www.vegasistemas.com.mx/vegabasic.html

Re: I can not return a string of a external DLL

Posted: Sat Jul 25, 2015 8:38 am
by H.Brill
years ago i had the same Problem with PowerBasic.
this declaration can help :
Dim Str1 AS ASCIIZ * 255

the string handling methods of some programming languages
are different, per example : the len of bytes at first

Or :

If you make your DLL with PowerBasic or others, take a
Memory pointer for the communication with strings between
the DLL and the program.

It is the better way and allways compatible.

Re: I can not return a string of a external DLL

Posted: Sat Jul 25, 2015 11:54 am
by Shardik
PowerBASIC is using BSTR strings, so you have to use the PureBasic Pseudotype P-BSTR to automatically convert PureBasic's zero terminated strings into PowerBASIC's BSTR strings. In your code example even your calculation returns the incorrect value 0 instead of 9992.7818 because you have incorrectly passed your strings as PureBasic strings!

I have tested the following example successfully with Windows XP SP3 and Windows 7 SP1 x64 with PB 5.31 x86 in ASCII and Unicode mode:

Code: Select all

#DLLName = "E:\Temp\VEGAEVAL.DLL" ; <-- Change the path to your DLL !!!

DLL = OpenLibrary(#PB_Any, #DLLName)

If DLL = 0
  MessageRequester("Error",
    "The loading of " + #DLLName + " failed!",
    #MB_ICONERROR)
Else
  Prototype.L ProtoExeBasic(Code.P-BSTR, Handle.L, Mode.L)
  ExeBasic.ProtoExeBasic=GetFunction(DLL, "ExeBasic")

  Prototype.L GetTextValue(String.P-BSTR)
  GetTextValue.GetTextValue = GetFunction(DLL, "GetTextValue")

  Prototype.L SetTextValue(String1.P-BSTR, String2.P-BSTR, Mode.L)
  SetTextValue.SetTextValue = GetFunction(DLL, "SetTextValue")

  Prototype.D ProtoGetValue(Var.P-BSTR)
  GetValue.ProtoGetValue=GetFunction(DLL, "GetValue")

  Prototype.L ProtoSetValue(Var.P-BSTR, Value.D, Mode.L)
  SetValue.ProtoSetValue=GetFunction(DLL, "SetValue")

  SetValue("A#", 100.25, 1)
  SetValue("B#", 1.50, 1)
  SetValue("C#", 999.99, 1)
  ExeBasic("C#=((A#+B#+C#)*18.14)/2", 0, 1)
  MessageRequester("Result", "C# = " + StrD(GetValue("C#")),
    #MB_ICONINFORMATION)

  SetTextValue("VAR1$", "ÄÖÜäöüßéà", 1) ; German Umlaut codes and French acute and grave accent
  SetTextValue("VAR2$", "?", 1)
  ExeBasic("VAR2$=VAR1$", 0, 1)
  MessageRequester("Result", "VAR2$ = " +
    PeekS(GetTextValue("VAR2$"), -1, #PB_Ascii),
    #MB_ICONINFORMATION)
 
  CloseLibrary(DLL)
EndIf
Interestingly in Windows XP the line

Code: Select all

  SetTextValue("VAR2$", "?", 1)
which defines VAR2$ was necessary because otherwise the program would crash. This line is not necessary in Windows 7...

Re: I can not return a string of a external DLL

Posted: Sat Jul 25, 2015 9:30 pm
by ivega718
This forum is great. Many thanks for your support.
Now works well with strings. The next thing will be to write this
utility in PureBasic.

Now also is possible use:

Code: Select all

  SetTextValue ("VAR1$","ISRAEL VEGA ALVAREZ",1)
  ExeBasic ("VAR2$=VAR1$",0,1)
  ExeBasic ("VAR3$=LEFT$(VAR2$,5)",0,1)
  SetTextValue ("DELIMIT$","VEGA",1)
  ExeBasic ("VAR4$=PARSE$(VAR1$,DELIMIT$,1)",0,1)

  MessageRequester("Result", "VAR4$ = " + PeekS(GetTextValue("VAR4$"), -1, #PB_Ascii), #MB_ICONINFORMATION)

and more sentences and functions with strings in Basic...