Solved

severe exception errors with dll

Posted on 2002-04-14
17
163 Views
Last Modified: 2011-09-20
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.

0
Comment
Question by:classic_gaming
  • 9
  • 7
17 Comments
 
LVL 1

Expert Comment

by:Alone
ID: 6941129
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
 
LVL 20

Expert Comment

by:Madshi
ID: 6941353
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
 

Author Comment

by:classic_gaming
ID: 6941511
ok i get incompatable types string and pchar

know a code to convert that? :-)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6941526
Please show us the code where you get this error.
0
 

Author Comment

by:classic_gaming
ID: 6941549
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
 
LVL 20

Expert Comment

by:Madshi
ID: 6941554
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
 

Author Comment

by:classic_gaming
ID: 6941558
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
 
LVL 20

Expert Comment

by:Madshi
ID: 6941576
Why did you change my code back? Try my code as it is, it should work that way.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:classic_gaming
ID: 6941577
i didnt see your post when i posted :-)

i still get a severe exception on the code plus a few errors :)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6941581
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
 

Author Comment

by:classic_gaming
ID: 6941586
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
 

Author Comment

by:classic_gaming
ID: 6941594
it errors when returning what i put in the textbox

so we have to find out how to return it without errors :)
0
 

Author Comment

by:classic_gaming
ID: 6941627
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
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 6941658
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
 

Author Comment

by:classic_gaming
ID: 6941887
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
 

Author Comment

by:classic_gaming
ID: 6941913
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
 
LVL 20

Expert Comment

by:Madshi
ID: 6942046
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now