• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 802
  • Last Modified:

Win32 dll into Delphi .Net

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
liljen_80
Asked:
liljen_80
  • 11
  • 11
  • 6
2 Solutions
 
2266180Commented:
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
 
liljen_80Author Commented:
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
 
2266180Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
liljen_80Author Commented:
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
 
liljen_80Author Commented:
This does not help me!!

Is it somone that know Delphi that can help me, please?
0
 
2266180Commented:
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
 
atul_parmarCommented:
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
 
2266180Commented:
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
 
atul_parmarCommented:
Well, this will work.

interface

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

implementation

function MessageBeep(uType : LongWord) : Boolean; external; 'user32.dll';
0
 
2266180Commented:
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
 
atul_parmarCommented:
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
 
2266180Commented:
I see. so I learned a new thing today. not bad :)
0
 
atul_parmarCommented:
I too learned many things from yours. The one is ...

"To stand ahead, learn everyday" :)
0
 
liljen_80Author Commented:
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
 
liljen_80Author Commented:
The dll is by the way located in the same direktory as the project.
0
 
2266180Commented:
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
 
atul_parmarCommented:
Should read as
var
  Form1: TForm1;
  function MessageBeep(uType : LongWord) : Boolean; external 'User32.dll';


FUNK
var
  Test:LongWord;
begin
  MessageBeep(Test);
0
 
liljen_80Author Commented:
It then gives the result Test = 0 and the MessageBeep(Test) = False.
What too do now?
0
 
liljen_80Author Commented:
And GetLastError gives 0 in return.
0
 
2266180Commented:
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
 
atul_parmarCommented:
var
  Test:LongWord;
begin
  Test := 0xFFFFFFFF;
  MessageBeep(Test);

other Possible values for Test
MB_ICONASTERISK      
MB_ICONEXCLAMATION
MB_ICONHAND
MB_ICONQUESTION
MB_OK
0
 
liljen_80Author Commented:
atul_parmar:
with  
  bTest := MessageBeep(Test);
  Testing := GetLastError();
bTest = false and Testing = 0 for all.

ciuly:
How too replase massagebeep?!?
0
 
2266180Commented:
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
 
liljen_80Author Commented:
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
 
2266180Commented:
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
 
liljen_80Author Commented:
Hehe... ofsause UsDef belongs to AntallVersjoner, only a when writing inside her.

And UsDef IS in the same directory as the EXE file...
0
 
2266180Commented:
is the dll ok? can you loaded with say dependency walker or other dll utility?
0
 
liljen_80Author Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now