Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing a string from a D3 exe to a BCB dll

Posted on 1997-12-26
7
Medium Priority
?
356 Views
Last Modified: 2010-05-18
I am trying to pass a string as an argument from a D3 exe to a BCB dll. The BCB docs indicate that if one uses a char pointer, it is not required to use bcbmm.dll. A doc at the Borland website, "Extending Delphi with DLLs" provides snippets illustrating how this is done. Unfortunately I can not get this to work.

BCB code:

extern "C" __pascal __declspec(dllexport) int AddVals(int nA, int nB, int nC, int nD)
{
      return (nA+nB+nC+nD);
}

/*
This is from "Extending Delphi with DLLs" found at the Borland website
*/

int far _export pascal PassCharPtr(char far* strA)
{
      MessageBox(NULL, strA, "GPC_TPL", MB_OK);
      return (strlen(strA));
}

D3 code:
  function ADDVALS(nA,nB,nC,nD: integer):integer; stdcall; far external 'GPC_TPL.DLL' index 2;
  function PASSCHARPTR(strParam: PChar):integer; external 'GPC_TPL' index 1;


var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
const
  StringLength = 255;
var
  nRetVal: integer;
  strA: array[0..StringLength] of char;
begin
  nRetVal := ADDVALS(1,2,3,4);
  ShowMessage(IntToStr(nRetVal));

  StrPCopy(strA, 'Hello World!');
  nRetVal := PASSCHARPTR(strA);
  ShowMessage(IntToStr(nRetVal));
end;

The PassCharPtr method in the dll does not receive the string as a argument and returns the length of the string as 3 no matter what the string length really is.

BCB suggests adding bcbmm.lib to both the dll and exe. I cannot seem to add bcbmm.lib to the D3 exe.

When I add ShareMem.pas to the dll project and put ShareMem at the head of my uses list, I get the same results plus when I close the exe an InvalidPointer exception is thrown.

Suggestions on how to successfully pass a string (<255 char) as an argument from a D3 exe to a BCB dll method will be greatly appreciated.

thanks
A.G.
0
Comment
Question by:AG112297
[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
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:altena
ID: 1354626
I remember having had a small fight with D2 just like you have
here.

Try this (and reject the answer if it does not work, I do not
remember exactly whenand why I had to pull this stunt)

replace:
nRetVal := PASSCHARPTR(strA);

with
nRetVal := PASSCHARPTR(&strA[0]);

and if it does work, well an "A" would be nice ;-)
0
 

Author Comment

by:AG112297
ID: 1354627
Unfortunately Delphi 3 informs me that the ampersand is an "illegal character in input file" and as such will not compile. Thank you for the effort.
0
 
LVL 3

Expert Comment

by:altena
ID: 1354628
Ok, so i use C++ now...

try again with the "@" character.

// If that also does not compile, I will actually fire up
// delphi and re-invent the "address off" operator :-)
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!

 

Author Comment

by:AG112297
ID: 1354629
The D3 compiles but when it executes and invokes the dll's PassCharPtr method, the results are unchanged. I have been trying to find more information on the calling convention as I think that is where the problem might lie. FWIW both methods work fine when invoked from a BCB exe.

Thanks again.
0
 
LVL 3

Expert Comment

by:altena
ID: 1354630
hmmm, I'll give this one last (this will be my last "answer") try:

Take another look at the Delphi declarations:
--
D3 code:
function ADDVALS(nA,nB,nC,nD: integer):integer; stdcall; far external 'GPC_TPL.DLL' index 2;
function PASSCHARPTR(strParam: PChar):integer; external 'GPC_TPL' index 1;
--
The second function, the one with the pchar, does NOT have a
"stdcall" keyword.

 Happy new year. (even if this also does not help)
0
 
LVL 3

Accepted Solution

by:
altena earned 300 total points
ID: 1354631
hmmm, I'll give this one last (this will be my last "answer") try:

Take another look at the Delphi declarations:
--
D3 code:
function ADDVALS(nA,nB,nC,nD: integer):integer; stdcall; far external 'GPC_TPL.DLL' index 2;
function PASSCHARPTR(strParam: PChar):integer; external 'GPC_TPL' index 1;
--
The second function, the one with the pchar, does NOT have a
"stdcall" keyword.

 Happy new year. (even if this also does not help)
0
 

Author Comment

by:AG112297
ID: 1354632
I figured that out this afternoon. I next ran into a problem when I 'combined' the two methods into one, DoItAll(char*,int,int,int,int). Using the calling conventions above, the method wouldn't work. I finally tried the basic convention,
int __export DoItAll(LPTSTR strA, int nA, int nB, int nC, int nD)
and it worked. The name is mangled but since I will call the method ordinally from D3 it doesn't matter to me. Thanks for the help.


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

721 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