how to use delphi dll in C#

R

Ryanivanka

hi,

I am using c# to call a DELPHI.DLL, but I don't think I do it
properly. Please help me check the codes:

I use the example way in MSDN to covert the unmanaged DLL to fit
for .net framework.

I try to use the "injectLibrary(..)" in DELPHI.DLL to inject a A.DLL
to a notepad.exe process.(the function is correct in vc60). if A.DLL
is loaded in any process,a messagebox will pop up.

firstly,I change the prototype for the two API from unmanaged DLL. one
is madchook.dll,the other is kernel32.dll.
************************************prototype for unmanaged
api************************************************
[DllImport("DELPHI.DLL", EntryPoint =
"InjectLibraryW",CallingConvention=CallingConvention.StdCall,SetLastError
=true )]
public extern static Boolean InjectLibrary
(
IntPtr dwProcessHandleOrSpecialFlags,
[MarshalAs(UnmanagedType.LPStr)]String pLibFileName,
IntPtr dwTimeOut
);

[DllImport ("kernel32.dll",EntryPoint
="LoadLibraryW",CallingConvention=CallingConvention.StdCall,SetLastError
=true)]
public extern static ModuleHandle LoadLibrary
(
[MarshalAs(UnmanagedType.LPTStr)]string lpFileName
);

**********************************************************************************************************************************


then,i call the two API .
**********************************call the two api in
c#***********************************************************************
bool result = false;
int err = 0;
Process[] p =
System.Diagnostics.Process.GetProcessesByName("notepad");
result = InjectLibrary(p[0].Handle, "A.dll",
(System.IntPtr)7000);
err=Marshal.GetLastWin32Error();
ModuleHandle mh = LoadLibrary("A.dll");
***********************************************************************************************************************************

the result is always false. the error code is 2;(even if the file path
is comlete and the notepad process is founded,the p[0].handle has
correct value).and i counldn't find code 2 in win32 error codes.
but the "loadlibrary(..)"call is perfectly right,I can see the pop up
messagebox.

so does anybody have a clue about this?
or just tell me how to use madcodehook in .net framework? should I
wrap the dll in vc++.net, then add it into C# project?

thank you very much.
 
R

Ryanivanka

The detail about the DELPHI.DLL is complicated.It's written in
delphi,but i has c++ prototype in .h file where its calling convention
is stdcall. and the lib files have two versions,one is called
"dynamics version" which means late binding , the other is "static
version". I am really confused by all these files, how should I use
them?
 
P

Pavel Minaev

The detail about the DELPHI.DLL is complicated.It's written in
delphi,but i has c++ prototype in .h file where its calling convention
is stdcall. and the lib files have two versions,one is called
"dynamics version" which means late binding , the other is "static
version". I am really confused by all these files, how should I use
them?

Can you please give the Delphi and/or C++ declarations for the
functions you're trying to call?

One thing that strikes me as odd in particular is that the funcitons
you call both end in "W", typically indicating a Unicode version; and
yet, you use [MarshalAs(UnmanagedType.LPStr)] - which is an ANSI
string - rather than [MarshalAs(UnmanagedType.LPWStr)] - which is a
Unicode string. Try the latter, and see if it helps.

By the way, you shouldn't even need MarshalAs for strings as used here
- they marshal as null-terminated by default, and whether the function
is ANSI or Unicode can be specified by DllImportAttribute.CharSet:

[DllImport("DELPHI.DLL",
EntryPoint = "InjectLibraryW",
CallingConvention = CallingConvention.StdCall,
SetLastError = true,
CharSet = CharSet.Unicode)]
 
S

sarah.hhyy

hi,Pavel

thank you very much ,the problem is about unicode.
because the "injectLibraryW()" I imported should be used in unicode, when I put "CharSet = CharSet.Unicode",everything is ok.

thank you again.



-----------------------------------------
ÔÚ Sun, 6 Jul 2008 23:48:07 -0700 (PDT) £¬Pavel MinaevдµÀ£º
The detail about the DELPHI.DLL is complicated.It's written in
delphi,but i has c++ prototype in .h file where its calling convention
is stdcall. and the lib files have two versions,one is called
"dynamics version" which means late binding , the other is "static
version". I am really confused by all these files, how should I use
them?

Can you please give the Delphi and/or C++ declarations for the
functions you're trying to call?

One thing that strikes me as odd in particular is that the funcitons
you call both end in "W", typically indicating a Unicode version; and
yet, you use [MarshalAs(UnmanagedType.LPStr)] - which is an ANSI
string - rather than [MarshalAs(UnmanagedType.LPWStr)] - which is a
Unicode string. Try the latter, and see if it helps.

By the way, you shouldn't even need MarshalAs for strings as used here
- they marshal as null-terminated by default, and whether the function
is ANSI or Unicode can be specified by DllImportAttribute.CharSet:

[DllImport("DELPHI.DLL",
EntryPoint = "InjectLibraryW",
CallingConvention = CallingConvention.StdCall,
SetLastError = true,
CharSet = CharSet.Unicode)]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top