473,378 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 3770
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Erva | last post by:
Hi, Is there someone who has moved from Delphi to VS.NET? I'am using Delphi currently but seriously considering to moving VS.NET. I would like to hear if someone has already done that, is it...
6
by: Erva | last post by:
Hi, Is there someone who has moved from Delphi to VS.NET? I'am using Delphi currently but seriously considering to moving VS.NET. I would like to hear if someone has already done that, is it...
1
by: Thomas Due | last post by:
Hi, I manage an rather old application in which we have some fairly complex (ugly) Delphi code. This is Delphi 6 we're talking about. Among all this Delphi code there is method for formating a...
4
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Dear List, We have a large application written in Delphi. I am writing a mobile app written in .NET (C#) which reads and writes into the Delphi applications Oracle database via a .NET Web...
11
by: gnuist006 | last post by:
Is there a Delphi equivalent in the C world or Scheme/LISP world ? Recently, Delphi is in resurgence. In Russia people are using like crazy. For example, Bolega has written a free image...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.