[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

severe exception errors with dll

Posted on 2002-04-14
17
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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