How to Access C DLLs from C#??

  • Thread starter Thread starter neeraj.krishna
  • Start date Start date
N

neeraj.krishna

Hi,
Is there anyone who has tried accessing C Dlls from C#? I got to know
that its Platform invocation Service, but did'nt get enough
documentation.
Can you give me some links to it?
Especially when a Struct is defined in C, how do I use it in C#. For
example, if there is a structure in C DDL(say cdll.dll) called struct
ListFields{}. How do use it in C# code??
Regards,
Neeraj Krishna
 
Hi,
Is there anyone who has tried accessing C Dlls from C#? I got to know
that its Platform invocation Service, but did'nt get enough
documentation.
Can you give me some links to it?
Especially when a Struct is defined in C, how do I use it in C#. For
example, if there is a structure in C DDL(say cdll.dll) called struct
ListFields{}. How do use it in C# code??
Regards,
Neeraj Krishna

Here's a simple example

[DllImport("user32")]
private static extern int GetWindowRect(int hwnd, ref RECT lpRect);

private class RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
 
Hi,
But my structure is defined in C.....That is
In C DLL i have:
typedef struct Rect {
int x,y;
} MYRECT

In C to use this we write:
MYRECT rect1;

But to use the same DLL in C# WHAT SHOULD I DO???
I don't want to write a class and redefine the elements in it

Please help me ???
Here's a simple example

[DllImport("user32")]
private static extern int GetWindowRect(int hwnd, ref RECT lpRect);

private class RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
 
C# cannot use types defined in a C DLL. You must redefine them in your C#
program.
Otherwise, you COULD create a COM/.NET wrapper in the C DLL for the C types
but it's a quite a lot of work.

Hi,
But my structure is defined in C.....That is
In C DLL i have:
typedef struct Rect {
int x,y;
} MYRECT

In C to use this we write:
MYRECT rect1;

But to use the same DLL in C# WHAT SHOULD I DO???
I don't want to write a class and redefine the elements in it

Please help me ???
Here's a simple example

[DllImport("user32")]
private static extern int GetWindowRect(int hwnd, ref RECT lpRect);

private class RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
 

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

Back
Top