C libraries in C#

G

Guest

Hi,
I am a student and new to .NET Compact Framework,Visual Studio.NET.
I am using .NET Compact Framework and Visual C#.NET for my project.
I have to use some C runtime DLLs in my project, but I have no idea
how this can be done. I mean, using C SDK and C runtime DLLs in
C# smart device applications template.
Can any one let me know wheather it( using C SDK and C runtime DLLs in C#
smart device application ) is possible or not.

If we can do this kindly let me know the procedure.

Thank you,

Naveen.
 
G

Guest

Hi Steve,

Thank you for your response.
I when I tried a samle program, I got "Missing method exception" in my
PDA.

I am wondering are we supposed to install and register the "DLL" in PDA.

Kindly let me know.

Cheers,

Naveen
 
G

Guest

Hi Awni,

Thank you for your response.
I when I tried a samle program, I got "Missing method exception" in my
PDA.

I am wondering are we supposed to install and register the "DLL" in PDA.

Kindly let me know.

Cheers,

Naveen
 
P

Paul G. Tobey [eMVP]

You don't have to register anything, but you should make sure that you are
compiling the exported functions in the DLL as C functions, not C++. C++,
as you certainly must know, changes the exported names of functions so that
their parameter and return types are part of their name. This allows
overloading of functions, since you can distinguish between int A( float b )
and int A( long b ) by the mangled function name, but it also makes it
harder to call from systems where you have to know the actual exported name.
The depends.exe tool will show you the actual exported names from a DLL and
you should be able to use this tool to see if what you are telling your C#
code to call is actually what the DLL is exporting...

Paul T.
 
G

Guest

Hi Paul,

Thank you very much for your reply.
As I am a starter, I couldn't understand
" but you should make sure that you are
compiling the exported functions in the DLL as C functions, not C++. "

I have a C run time DLL( third party), I need to access the C methods in
this DLL. What I have done is
I have written a sample code which access a method called Connect in the dll.

[ DllImport (" blah.dll")]
private static extern void Connect ();

Then I compiled the code using "Build"
but when I deployed it on my PDA, it is giving "MissingMethod" exception.

Am I doing right thing?. if not, kindly let me know what I need to change.
do I need to change any settings in Visual Studio.NET 2003.

I got the same problem when I tried the following sample code from the
internet

using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

public class Win32
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(int hWnd, string text,
string caption, uint type);
}

public class HelloWorld
{
public static void Main()
{
Win32.MessageBox(0, "Hello World", "Platform InvokeSample", 0);
}
}


Thank you,

Naveen.
 
P

Paul G. Tobey [eMVP]

You need to learn the items that I mentioned in order to solve your problem.
You are sure that this third party compiled the DLL with C and not C++? I
didn't think so. If it *was* compiled with C++, there is no exported
function called Connect, so your code can't find it. You need to verify
that it is exporting the method with exactly that name. The depends.exe
tool, as I mentioned, will show you the exports.

Paul T.

Naveen Mukkelli said:
Hi Paul,

Thank you very much for your reply.
As I am a starter, I couldn't understand
" but you should make sure that you are
compiling the exported functions in the DLL as C functions, not C++. "

I have a C run time DLL( third party), I need to access the C methods in
this DLL. What I have done is
I have written a sample code which access a method called Connect in the
dll.

[ DllImport (" blah.dll")]
private static extern void Connect ();

Then I compiled the code using "Build"
but when I deployed it on my PDA, it is giving "MissingMethod" exception.

Am I doing right thing?. if not, kindly let me know what I need to change.
do I need to change any settings in Visual Studio.NET 2003.

I got the same problem when I tried the following sample code from the
internet

using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

public class Win32
{
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(int hWnd, string text,
string caption, uint type);
}

public class HelloWorld
{
public static void Main()
{
Win32.MessageBox(0, "Hello World", "Platform InvokeSample", 0);
}
}


Thank you,

Naveen.

Paul G. Tobey said:
You don't have to register anything, but you should make sure that you
are
compiling the exported functions in the DLL as C functions, not C++.
C++,
as you certainly must know, changes the exported names of functions so
that
their parameter and return types are part of their name. This allows
overloading of functions, since you can distinguish between int A( float
b )
and int A( long b ) by the mangled function name, but it also makes it
harder to call from systems where you have to know the actual exported
name.
The depends.exe tool will show you the actual exported names from a DLL
and
you should be able to use this tool to see if what you are telling your
C#
code to call is actually what the DLL is exporting...

Paul T.
 
G

Guest

Hi Steve,

How can we send string to the unmanaged C method( in dll) which requires
char *.

for example C method in dll

char *someMethod( char *name ); this method
takes a "char" pointer and returns a "char *"

When I tryied in following way, it is not working..

// decleration
[DllImport("somedll.dll")]
public static extern string someMethod( string name );

// method invocation
string str = someMethod("message");
MessageBox.Show(str);

am I missing any thing? is there any way we can send string to the
unmanaged methods and handle char * from unmanaged code using C#?

kindly let me know.

Cheers,

Naveen.
 
C

Chris Tacke, eMVP

In CE strings are Unicode, so they're not chars, but rather wchar_t. Either
you must change the unmanaged function to use Unicode strings, or you must
convert the strings to ANSI before passing them over.

-Chris


Naveen Mukkelli said:
Hi Steve,

How can we send string to the unmanaged C method( in dll) which requires
char *.

for example C method in dll

char *someMethod( char *name ); this method
takes a "char" pointer and returns a "char *"

When I tryied in following way, it is not working..

// decleration
[DllImport("somedll.dll")]
public static extern string someMethod( string name );

// method invocation
string str = someMethod("message");
MessageBox.Show(str);

am I missing any thing? is there any way we can send string to the
unmanaged methods and handle char * from unmanaged code using C#?

kindly let me know.

Cheers,

Naveen.

Steve Maillet (eMVP) said:
In particular search for "dll import" or "PInvoke"

--
Steve Maillet
EmbeddedFusion
www.EmbeddedFusion.com

Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/community/newsgroups
 
G

Guest

Hi Chris,

Thanks for your suggestion.
It works well now.
I have converted the string to unicode byte array and sent the
byte array to the dll. and it works. fine.

Cheers

Chris Tacke said:
In CE strings are Unicode, so they're not chars, but rather wchar_t. Either
you must change the unmanaged function to use Unicode strings, or you must
convert the strings to ANSI before passing them over.

-Chris


Naveen Mukkelli said:
Hi Steve,

How can we send string to the unmanaged C method( in dll) which requires
char *.

for example C method in dll

char *someMethod( char *name ); this method
takes a "char" pointer and returns a "char *"

When I tryied in following way, it is not working..

// decleration
[DllImport("somedll.dll")]
public static extern string someMethod( string name );

// method invocation
string str = someMethod("message");
MessageBox.Show(str);

am I missing any thing? is there any way we can send string to the
unmanaged methods and handle char * from unmanaged code using C#?

kindly let me know.

Cheers,

Naveen.

Steve Maillet (eMVP) said:
In particular search for "dll import" or "PInvoke"

--
Steve Maillet
EmbeddedFusion
www.EmbeddedFusion.com

Do have an opinion on the effectiveness of Microsoft Windows Mobile and
Embedded newsgroups? Let us know!
https://www.windowsembeddedeval.com/community/newsgroups
 

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

Top