severe exception errors with dll

hi,

i'm trying to figure out where i'm going wrong, here is the code to create a dll

when i use it in another language it errors but works.
like i can see the showmessage and the inputbox but when clicking ok it errors.

let me know where i'm going wrong

cheers

code:

// if you search for a component in the help files you'll find uses in there so you know what lib that component is under.
// e.g. when making applications you need "Forms"
  Sharemem,
  //SysUtils,
  //Classes,
  Dialogs;

var
  input: string;

procedure showtest(name: string); stdcall
begin
// this will do a basic message box with the string you sent it.
showmessage(name);
end;

procedure inputtest(caption: string; prompt: string; text: string); stdcall
begin
// this will show a inputbox with the details you provided above.
input := InputBox(caption,prompt,text);
{
example.
/-----------------\
¦ caption         ¦
¦-----------------¦
¦ prompt          ¦
¦   text          ¦
\-----------------/
}
end;

// this command will export all the functions used so the user using the dll can call a function.
exports showtest,inputtest;
end.

classic_gamingAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
MadshiConnect With a Mentor Commented:
Ooops, my fault, please remove that "var" from the "inputtest" declaration!

procedure inputtest(caption, prompt, text, input: pchar); stdcall;

This way it should finally work.
0
 
AloneCommented:
What "another language"? C++ or Basic? ;-)
IMHO, if you're using this DLL from a program not written on Delphi, you must use PChar instead of Delphi string.
Default Delphi memory manager uses non-standard memory allocation for long strings. You need ShareMem unit only you're using this DLL in a Delphi program. Program written in "another language" conflicts with Delphi memory allocation and causes errors.

WBR...
0
 
MadshiCommented:
Alone is right. Try getting rid of ShareMem and use PChar instead of string.

In case you really want to use string, you should note, that this is not compatible to VB or MSCV++, furthermore you then need ShareMem, and you need to add it to the first uses location in your PROJECT (*.dpr), not the unit.

Regards, Madshi.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
classic_gamingAuthor Commented:
ok i get incompatable types string and pchar

know a code to convert that? :-)
0
 
MadshiCommented:
Please show us the code where you get this error.
0
 
classic_gamingAuthor Commented:
this line:
input := InputBox(caption,prompt,text);
i updated the source to look like this:

uses
  Dialogs;

var
  input: pchar;

procedure showtest(name: string); stdcall
begin
showmessage(name);
end;

function inputtest(caption: string; prompt: string; text: string): pchar; stdcall
begin
input := InputBox(caption,prompt,text);
end;

exports showtest,inputtest;
end.
0
 
MadshiCommented:
It should look like this:

var input : array [0..255] of char;
begin
  InputBox(pchar(caption), pchar(prompt), pchar(text), @input);

------------------

uses
 Dialogs;

procedure showtest(name: pchar); stdcall;
begin
  showmessage(string(name));
end;

procedure inputtest(caption, prompt, text: pchar; var input: pchar); stdcall;
begin
  StrPLCopy(input, InputBox(caption,prompt,text), 255);
end;

exports showtest,inputtest;

end.
0
 
classic_gamingAuthor Commented:
ah i changed it to this now it works:

input := pchar(InputBox(caption,prompt,text));

i still get severe exception on it :-(

---------- code ------

library dialog;

uses
  Dialogs;

var
  input: pchar;

procedure showtest(name: string); stdcall
begin
showmessage(name);
end;

function inputtest(caption: string; prompt: string; text: string): pchar; stdcall
begin
input := pchar(InputBox(caption,prompt,text));
end;

exports showtest,inputtest;
end.
0
 
MadshiCommented:
Why did you change my code back? Try my code as it is, it should work that way.
0
 
classic_gamingAuthor Commented:
i didnt see your post when i posted :-)

i still get a severe exception on the code plus a few errors :)
0
 
MadshiCommented:
Then please post the exception text here together with in which line it occurs. Also show me the full source code that you have now. That is application code and dll code.
0
 
classic_gamingAuthor Commented:
library dialognew;

//InputBox(pchar(caption), pchar(prompt), pchar(text), @input);

uses
  Dialogs,
  Sysutils;

var
  input : array [0..255] of char;

procedure showtest(name: pchar); stdcall;
begin
 showmessage(string(name));
end;

procedure inputtest(caption, prompt, text: pchar; var input: pchar); stdcall;
begin
 StrPLCopy(input, InputBox(caption,prompt,text), 255);
end;

exports showtest,inputtest;

end.

and the application is not in delphi, it's darkbasic ;)

if you want the darkbasic source just ask
0
 
classic_gamingAuthor Commented:
it errors when returning what i put in the textbox

so we have to find out how to return it without errors :)
0
 
classic_gamingAuthor Commented:
library dialognew;

//InputBox(pchar(caption), pchar(prompt), pchar(text), @input);

uses
  Dialogs,
  Sysutils;

//var
  //input : array [0..255] of char;

procedure showtest(name: pchar); stdcall;
begin
 showmessage(string(name));
end;

function inputtest(caption, prompt, text: pchar; var input: pchar): pchar; stdcall;
begin
input := pchar(InputBox(pchar(caption), pchar(prompt), pchar(text)));
//StrPLCopy(input, InputBox(caption,prompt,text), 255);
end;

exports showtest,inputtest;

end.
0
 
classic_gamingAuthor Commented:
right this seems to work:

procedure inputtest(caption, prompt, text: pchar); stdcall;
begin
InputBox(pchar(caption), pchar(prompt), pchar(text));
end;

but i need to make it a function now and it need to return what is in input.

cheers
0
 
classic_gamingAuthor Commented:
ah figured it out :)

function inputdialog(caption, prompt, text: pchar): pchar; stdcall
var
ppchar: pchar;
begin
ppchar := pchar(inputbox(pchar(caption), pchar(prompt), pchar(text)));
result := ppchar;
end;

i'll give you the poinbts for helping me :)

cheers
0
 
MadshiCommented:
Sorry to say, but this is not a good solution, it's dangerous, it may end up in an exception sooner or later. Look: InputBox gives back a Delphi string, and Delphi frees such strings automatically, if they're not needed anymore. When leaving the InputDialog function Delphi will free the string, so your pchar return points to an already freed string. That might work most of the time, but it's really dangerous. You can't simply return pchars. Look at all the win32 APIs, nobody returns a pchar there. You always give in the result pchar as a parameter into the function. E.g. see GetModuleFileName. And you'll have to do it the same way. See my code, there the result does come back, but as the forth parameter.

You should use the code from my comment from the "04/15/2002 03:25AM PST", just remove the "var" from the procedure definition.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.