473,404 Members | 2,137 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,404 software developers and data experts.

How to import function from C++ dll and use them in c# code ?

How cann I import & use these function from an old C++ dll ?

Only this, I've got are these two signatures of function I wannt use in
my C# app.

DllExport int DLLAPI drawImage( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
HANDLE &imageHandle, int &imageBufferSize,
char *imageFormat) ;

DllExport int DLLAPI drawImageFile( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
char *fileName,
char *imageFormat);
Jul 17 '07 #1
3 1912
Hi,

You have to use a feature named P/invoke, you can find a lot of examples in
pinvoke.net

"MichalSody" <m.*******@seznam.czwrote in message
news:Oo**************@TK2MSFTNGP05.phx.gbl...
How cann I import & use these function from an old C++ dll ?

Only this, I've got are these two signatures of function I wannt use in my
C# app.

DllExport int DLLAPI drawImage( char *chemBuffer, char *queryBuffer, char
*chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
HANDLE &imageHandle, int &imageBufferSize,
char *imageFormat) ;

DllExport int DLLAPI drawImageFile( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
char *fileName,
char *imageFormat);

Jul 17 '07 #2
MichalSody,

You won't be able to use the first function, as you can not access
referenced parameters (&) in .NET through the P/Invoke layer. You have to
create another version of the function (or a wrapper in unmanaged code if
you cant change the original) which uses pointers.

Assuming you have made the changes to the first function, your
declarations would be

[DllImport("<dllname>")]
static extern int drawImage(
[MarshalAs(UnmanagedType.LPStr)]
string chemBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string queryBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string chemFormat,
int showMapps,
int showRkz,
int showSter,
int showNumb,
int showInvRet,
int queryDisplay
int showResidue,
int canvasWidth,
int canvasHeight,
IntPtr imageHandle,
ref int imageBufferSize,
[MarshalAs(UnmanagedType.LPStr)]
string imageFormat);

[DllImport("<dllname>")]
static extern int drawImageFile(
[MarshalAs(UnmanagedType.LPStr)]
string chemBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string queryBuffer,
[MarshalAs(UnmanagedType.LPStr)]
string chemFormat,
int showMapps,
int showRkz,
int showSter,
int showNumb,
int showInvRet,
int queryDisplay
int showResidue,
int canvasWidth,
int canvasHeight,
[MarshalAs(UnmanagedType.LPStr)]
string fileName,
[MarshalAs(UnmanagedType.LPStr)]
string imageFormat);

These declarations assume that you are not modifying the contents of the
string parameters at all. If you are, then you need to change the
declarations to a StringBuilder instance and make sure to initialize it to
have enough capacity to be written to by the unmanaged function.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
DllExport int DLLAPI drawImage( char *chemBuffer, char *queryBuffer, char
*chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
HANDLE &imageHandle, int &imageBufferSize,
char *imageFormat) ;

DllExport int DLLAPI drawImageFile( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
char *fileName,
char *imageFormat);
"MichalSody" <m.*******@seznam.czwrote in message
news:Oo**************@TK2MSFTNGP05.phx.gbl...
How cann I import & use these function from an old C++ dll ?

Only this, I've got are these two signatures of function I wannt use in my
C# app.

DllExport int DLLAPI drawImage( char *chemBuffer, char *queryBuffer, char
*chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
HANDLE &imageHandle, int &imageBufferSize,
char *imageFormat) ;

DllExport int DLLAPI drawImageFile( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
char *fileName,
char *imageFormat);

Jul 17 '07 #3
Hi,

This article today may help: "Call Unmanaged DLLs from C#, Killing
Processes Cleanly":

http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

RKW.
On Jul 17, 10:14 pm, MichalSody <m.sodo...@seznam.czwrote:
How cann I import & use these function from an old C++ dll ?

Only this, I've got are these two signatures of function I wannt use in
my C# app.

DllExport int DLLAPI drawImage( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
HANDLE &imageHandle, int &imageBufferSize,
char *imageFormat) ;

DllExport int DLLAPI drawImageFile( char *chemBuffer, char *queryBuffer,
char *chemFormat,
int showMapps, int showRkz,
int showSter, int showNumb,
int showInvRet, int queryDisplay,
int showResidue,
int canvasWidth,int canvasHeight,
char *fileName,
char *imageFormat);

Jul 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul Rubin | last post by:
That's what the Python style guides advise. They don't seem to like def frob(x): import re if re.search('sdfxyz', x): ... instead preferring that you pollute your module's global namespace...
3
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also...
2
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the...
1
by: Dan | last post by:
Could someone please help me with auto importing a series of data files into an Access table. I tried to follow code given below in a previous messagebut i'm getting error messages. Here's my...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
1
by: baling | last post by:
Hi.... Hi everybody, i have a code that i make in VBA and know I want to use this code in to VB6. But i don't know how to use that code in to VB 6.0 Please correct this code so i can use it in VB...
6
by: bvdp | last post by:
I'm going quite nutty here with an import problem. I've got a fairly complicated program (about 12,000 lines in 34 modules). I just made some "improvements" and get the following error: bob$ mma...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.