Solved

Win32 dll into Delphi .Net

Posted on 2006-06-14
30
781 Views
Last Modified: 2010-04-16
Hi,

I have a problem with gething Win32 dll into a Delphi .Net. (I'm using Borland 2006)

I have found in the help:
You can call unmanaged Win32 APIs (and other unmanaged code) by prefixing the function declaration with the DllImport custom attribute. This attribute resides in the System.Runtime.InteropServices namespace, as shown below:
Eksempel:
[DllImport('user32.dll')]
function MessageBeep(uType : LongWord) : Boolean; external;

But I got the error:  Cannot have both a DLLImport attribute and an external or calling convention directive (E2293)

I haven't added other dll's, but I think it is somone that is there from the start. I have tryed with bouth .Net VCL Forms Application and .Net Windows Forms Application. But it is  .Net VCL Forms Application I want too use.

A bit more describing of the error:
The compiler emits DLLImport attributes internally for external function declarations. This error is raised if you declare your own DLLImport attribute on a function and use the external name clause on the function.

I have also tryed to use "ADD REFERANCE"; but I got the error: "user32.dll is not a valid assembly, type library, or COM object."

Anybody know how too solve this problem, pleace?
0
Comment
Question by:liljen_80
  • 11
  • 11
  • 6
30 Comments
 
LVL 28

Expert Comment

by:ciuly
ID: 16900588
just out of curiosity (never worked in delphi .net):
if you create a blank new app and add that example with messagebeep, does it still spit error? if so, remove the external and compile and run (put a messagebeep(3000) at form create ro somewhere and see if it works. if it works, you found the solution
0
 

Author Comment

by:liljen_80
ID: 16900655
ciuly: I have never tryed messagebeep, and I don't know how this would help. Because I get the error under compailing and I dont get too run the program at all.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16900971
that messagebeep thig is just a test. most issues are fixed by trying something that you know it should work.
there are 2 options:
- either the docuemntation is wrong
- or your code is wrong.
so just to those steps I said and let me know of the results
0
 

Author Comment

by:liljen_80
ID: 16900991
This does not help me!! I know I'm doing somthing wrong but not WHAT!

Is it somone that know Delphi that can help me, please?
0
 

Author Comment

by:liljen_80
ID: 16900993
This does not help me!!

Is it somone that know Delphi that can help me, please?
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16901153
it is your choise how you want to be helped. aprox 30% of my helpings were given without me knowing anything on the subject. in those cases, the askers cooperated and got their problem fixed fast.
of course, ti happens pretty often that someone who knows a lot on the subject will be online in a short period of time.
as I said, it is your choise.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16901179
As the error says try after removing one of them
e.g. do one of the following
1.
[DllImport('user32.dll')]
function MessageBeep(uType : LongWord) : Boolean;
OR
2.function MessageBeep(uType : LongWord) : Boolean; external;
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16901188
atul, I suggested the first already :P
the second has no real value since the dll is not specified (and it is about dynamic linking ;) )
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 25 total points
ID: 16901400
Well, this will work.

interface

function MessageBeep(uType : LongWord) : Boolean; cdecl;

implementation

function MessageBeep(uType : LongWord) : Boolean; external; 'user32.dll';
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16901443
atul, keep in mind that this has to compile and run in .net environment (.Net VCL Forms to quote the asker). are you sure it works in .net?
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16901491
Yeah. Ciuly the same will work as long as it does not have any textual parameter/return type.

There are two ways to call Win32 functions inside delphi
1. Traditional Delphi way (the sample I shown - In this case the Delphi.Net compiler takes care of adding DLLImport attribute)
2. By using the DLLImport attribute. (as liljen_80 has done. But sure he is missing to set some parameters of DLLImport - I don't have D2005/06 here else I can tell exactly what's wrong)

It should look like
DllImport('user32.dll', CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
function MessageBeep(uType : LongWord) : Boolean;
external;

0
 
LVL 28

Expert Comment

by:ciuly
ID: 16901507
I see. so I learned a new thing today. not bad :)
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16901624
I too learned many things from yours. The one is ...

"To stand ahead, learn everyday" :)
0
 

Author Comment

by:liljen_80
ID: 16902078
When I try too use
var
  Form1: TForm1;
  function MessageBeep(uType : LongWord) : Boolean; external 'UsDef.dll';


FUNK
var
  Test:LongWord;
begin
  MessageBeep(Test);


I get the error: Unable to load DLL(UsDef.dll)
0
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

 

Author Comment

by:liljen_80
ID: 16902090
The dll is by the way located in the same direktory as the project.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16902095
is usdef.dll your dll that you are trying to load? because messagebepp is declared in user32.dll so it makes no sence in trying to  load it from usdef.dll
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16902110
Should read as
var
  Form1: TForm1;
  function MessageBeep(uType : LongWord) : Boolean; external 'User32.dll';


FUNK
var
  Test:LongWord;
begin
  MessageBeep(Test);
0
 

Author Comment

by:liljen_80
ID: 16902193
It then gives the result Test = 0 and the MessageBeep(Test) = False.
What too do now?
0
 

Author Comment

by:liljen_80
ID: 16902231
And GetLastError gives 0 in return.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16902303
now you should replace "messagebeep" and param definitions with the ones of your function/procedure and "user32.dll" with your dll (I suppose usdef.dll) do that for each function yuo want to import
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16902320
var
  Test:LongWord;
begin
  Test := 0xFFFFFFFF;
  MessageBeep(Test);

other Possible values for Test
MB_ICONASTERISK      
MB_ICONEXCLAMATION
MB_ICONHAND
MB_ICONQUESTION
MB_OK
0
 

Author Comment

by:liljen_80
ID: 16902651
atul_parmar:
with  
  bTest := MessageBeep(Test);
  Testing := GetLastError();
bTest = false and Testing = 0 for all.

ciuly:
How too replase massagebeep?!?
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16902680
I thought you are trying to load some custom function, and that messagebeep is just an example. when I said replace, I meant that instead of
 function MessageBeep(uType : LongWord) : Boolean; external 'User32.dll';
write soemthing like:
function myfunc(params):resulttype; external 'mydll.dll';
but now I am not exactly sure what you want
0
 

Author Comment

by:liljen_80
ID: 16902705
ciuly:
If you mean too put in my DLL, like this:

var
  Form1: TForm1;
  function AntallVersjoner : longint; external 'User32.dll';
  function MessageBeep(uType : LongWord) : Boolean; external 'UsDef.dll';


FUNK
var
  Test:LongWord;
begin
  AntVersjoner := AntallVersjoner();
  bTest := MessageBeep(Test);
  Testing := GetLastError();
end;


I get the error: Unable to load DLL(UsDef.dll)
0
 
LVL 28

Expert Comment

by:ciuly
ID: 16902736
maybe instead of :
  function AntallVersjoner : longint; external 'User32.dll';
  function MessageBeep(uType : LongWord) : Boolean; external 'UsDef.dll';
should be
  function AntallVersjoner : longint; external 'UsDef.dll';
  function MessageBeep(uType : LongWord) : Boolean; external 'User32.dll';

then, you must make sure that UsDef.dll is in the same directory as the EXE, not the project. so compile the project, find the exe (see project output in project confiugration) and put the usdef.dll there (or get teh exe and put it with the dll in another directory)
0
 

Author Comment

by:liljen_80
ID: 16902841
Hehe... ofsause UsDef belongs to AntallVersjoner, only a when writing inside her.

And UsDef IS in the same directory as the EXE file...
0
 
LVL 28

Assisted Solution

by:ciuly
ciuly earned 25 total points
ID: 16902917
is the dll ok? can you loaded with say dependency walker or other dll utility?
0
 

Author Comment

by:liljen_80
ID: 16909580
This is incredible. The dll I got to work with linked to another dll that I was missing. But I had to start it direktly from the .exe file to find out that....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

12 Experts available now in Live!

Get 1:1 Help Now