471,349 Members | 1,271 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

how to use delphi dll in C#

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=CallingConventi on.StdCall,SetLastError
=true )]
public extern static Boolean InjectLibrary
(
IntPtr dwProcessHandleOrSpecialFlags,
[MarshalAs(UnmanagedType.LPStr)]String pLibFileName,
IntPtr dwTimeOut
);

[DllImport ("kernel32.dll",EntryPoint
="LoadLibraryW",CallingConvention=CallingConventio n.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("not epad");
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.
Jul 7 '08 #1
3 3660
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?
Jul 7 '08 #2
On Jul 7, 6:37*am, Ryanivanka <sarah.h...@gmail.comwrote:
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)]
Jul 7 '08 #3
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д
>On Jul 7, 6:37*am, Ryanivanka <sarah.h...@gmail.comwrote:
>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)]
-----------------------------------------


Ryanivanka
sa********@gmail.com

Jul 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Erva | last post: by
6 posts views Thread by Erva | last post: by
4 posts views Thread by =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.