THE ZEPINT NETWORK

programmer assist

C# C# XML Feeds

C# Questions C# Solutions C# Articles

C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Interfacing from C# application to a C DLL librray

DiggBlinkRedditDeliciousTechnorati

question by ellie | Easy

I have a C# application which takes strings and integer from user and pass them to a C DLL library, I have a difficult time to print the information in the DLL as it seems like there is pointer mis-adjustement.
In C# I have a structure which has two fields:
name
password.
when I pass the information(structure) to the C Dll to process I do not see the password.
my question? what is the best way to pass information from C# to a C DLL? can you refer me to sample files/a complete but simple C# application and C dll
thanks so much..

Post reply Subscriptions

Re: interfacing from C# application to a C DLL librray

reply by Bejaan

Use DLLImport and Extern.

Indicates that the attributed method is implemented as an export from an
unmanaged DLL.

For a list of all members of this type, see DllImportAttribute Members.

System.Object
System.Attribute
System.Runtime.InteropServices.DllImportAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Method)>
NotInheritable Public Class DllImportAttribute
Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Method)]
public sealed class DllImportAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets.Method)]
public __gc __sealed class DllImportAttribute : public Attribute
[JScript]
public
AttributeUsage(AttributeTargets.Method)
class DllImportAttribute extends Attribute
Thread Safety
Any public static (Shared in Visual Basic) members of this type are safe for
multithreaded operations. Any instance members are not guaranteed to be
thread safe.

Remarks
You can apply this attribute to methods.

This attribute provides the information needed to call an exported function
in an unmanaged DLL.

Example
[Visual Basic]
<DllImport("KERNEL32.DLL", EntryPoint := "MoveFileW", _
SetLastError := True, CharSet := CharSet.Unicode, _
ExactSpelling := True, _
CallingConvention := CallingConvention.StdCall)> _
Public Shared Function MoveFile(src As String, dst As String) As Boolean
' Leave function empty - DLLImport attribute forwards calls to MoveFile
to
' MoveFileW in KERNEL32.DLL.
End Function
[C#]
[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);
[C++, JScript] No example is available for C++ or JScript. To view a Visual
Basic or C# example, click the Language Filter button in the upper-left
corner of the page.

Requirements
Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server
family

Assembly: Mscorlib (in Mscorlib.dll)

Use the extern modifier in a method declaration to indicate that the method
is implemented externally. A common use of the extern modifier is with the
DllImport attribute. (See B.8 The DllImport attribute for more information.)

It is an error to use the abstract and extern modifiers together to modify
the same member. Using the extern modifier means that the method is
implemented outside the C# code, while using the abstract modifier means
that the method implementation is not provided in the class.

Because an external method declaration provides no actual implementation,
there is no method body; the method declaration simply ends with a semicolon
and there are no braces ({ }) following the signature. For example:

public static extern int MyMethod(int x);
Note The extern keyword is more limited in use than in C++. To compare
with the C++ keyword, see Using extern to Specify Linkage in the C++
Language Reference.
For more information on external methods, see 10.5.7 External methods.

For more information on attributes, see 17. Attributes.

Example
In this example, the program receives a string from the user and displays it
inside a message box. The program uses the MessageBox method imported from
the User32.dll library.

using System;
using System.Runtime.InteropServices;
class MyClass
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);

public static int Main()
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}

}

Sample RunEnter your message: Where do you want to go today?When the
previous text is entered, a message box that contains the text will pop up
on the screen.ExampleThis example uses two files, CM.cs and Cmdll.c, to
demonstrate extern. The C file is an external DLL that is invoked from
within the C# program. File: Cmdll.c// cmdll.c
// compile with: /LD /MD
int __declspec(dllexport) MyMethod(int i)
{
return i*10;
}File: CM.cs// cm.cs

using System;
using System.Runtime.InteropServices;
public class MyClass
{
[DllImport("Cmdll.dll")]
public static extern int MyMethod(int x);
public static void Main()
{
Console.WriteLine("MyMethod() returns {0}.", MyMethod(5));
}
}OutputMyMethod() returns 50.CompilationTo build the project, use the

following steps: a.. Compile Cmdll.c to a DLL using the Visual C++ command
line: cl /LD /MD Cmdll.cb.. Compile CM.cs using the command line: csc
CM.csThis will create the executable file CM.exe. When you run this program,
MyMethod will pass the value 5 to the DLL file, which returns the value
multiplied by 10.

Post reply Subscriptions

Re: Interfacing from C# application to a C DLL librray

reply by rednael

Please read the following article:
Marshalling: Using native DLLs in .NET
http://blog.rednael.com/2008/08/29/MarshallingUsingNativeDLLsInNET.aspx

It's an in-depth article about how to use a native DLL (or C++ DLL) in your managed .Net code. The article shows which types are interoperable, how to import a DLL, how to pass strings, how to pass structures and how to de-reference pointers.

And C# source code examples are included.

Post reply Subscriptions

Got a C# Question?

Just Sign Up and ask the top C# experts!

Search via Google

User Login

Email Address

Password

C# Experts

Rank Expert Points
#1 csfreak 1000
This a list of the Top C# experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 poizn 100
This is a list of overall best performing experts, how many points do you have?