problem in using 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=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.
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?
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.
date: Sun, 6 Jul 2008 19:32:59 -0700 (PDT)
author: Ryanivanka