Accessing C code from C#, best way. 
Author Message
 Accessing C code from C#, best way.

Hi,

I've got the C open source code for a useful program called Tidy which
turns malformed html into well formed 'tidied' html or even xml. The
Tidy source is quite large so I don't want to port it to C#. But I do
need to access its functions from C#.

I tried making a dll from Visual C++ V.6 with the C code and then using
dllimport in C# like this:

[DllImport("Win32DynaLnkLib.dll")]
extern void ParseConfigFile(char *file);

Adapted from the following which I used to set the system time in .net:

[DllImport("kernel32.dll")]
static extern bool SetSystemTime(ref SYSTEMTIME time);

But the C# compiler first didn't like the 'char *' in ParseConfigFile
and when I tried a simple type method an unhandled exception occurred:

Unhandled Exception: System.EntryPointNotFoundException: Unable to find
an entry point named IsLetter in DLL Win32DynaLnkLib.dll.
   at Test.IsLetter(UInt32 c)
   at Test.Main()

Can someone suggest how to sort this out or a better way of using C code
from C# .net.

Many thanks and regards,

..matthew



Fri, 19 Nov 2004 22:20:19 GMT  
 Accessing C code from C#, best way.
Matt,

    The problem stems from the way that strings are represented in C#
compared to C.  The declaration for your function would be as follows:

[DllImport("Win32DynaLnkLib.dll")]
public static extern void ParseConfigFile([MarshalAs(UmanagedType.LPStr)]
string file);

    For more information on how to marshal strings to unmanaged code, check
out the section of the .NET framework documentation titled "Marshaling
Strings", located at (watch for line wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...
l/cpconmarshalingstrings.asp

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi,

> I've got the C open source code for a useful program called Tidy which
> turns malformed html into well formed 'tidied' html or even xml. The
> Tidy source is quite large so I don't want to port it to C#. But I do
> need to access its functions from C#.

> I tried making a dll from Visual C++ V.6 with the C code and then using
> dllimport in C# like this:

> [DllImport("Win32DynaLnkLib.dll")]
> extern void ParseConfigFile(char *file);

> Adapted from the following which I used to set the system time in .net:

> [DllImport("kernel32.dll")]
> static extern bool SetSystemTime(ref SYSTEMTIME time);

> But the C# compiler first didn't like the 'char *' in ParseConfigFile
> and when I tried a simple type method an unhandled exception occurred:

> Unhandled Exception: System.EntryPointNotFoundException: Unable to find
> an entry point named IsLetter in DLL Win32DynaLnkLib.dll.
>    at Test.IsLetter(UInt32 c)
>    at Test.Main()

> Can someone suggest how to sort this out or a better way of using C code
> from C# .net.

> Many thanks and regards,

> ..matthew



Fri, 19 Nov 2004 22:27:13 GMT  
 Accessing C code from C#, best way.
try

[DllImport("Win32DynaLnkLib.dll")]
extern void ParseConfigFile(ref char file);

or try without ref


Quote:
> Hi,

> I've got the C open source code for a useful program called Tidy which
> turns malformed html into well formed 'tidied' html or even xml. The
> Tidy source is quite large so I don't want to port it to C#. But I do
> need to access its functions from C#.

> I tried making a dll from Visual C++ V.6 with the C code and then using
> dllimport in C# like this:

> [DllImport("Win32DynaLnkLib.dll")]
> extern void ParseConfigFile(char *file);

> Adapted from the following which I used to set the system time in .net:

> [DllImport("kernel32.dll")]
> static extern bool SetSystemTime(ref SYSTEMTIME time);

> But the C# compiler first didn't like the 'char *' in ParseConfigFile
> and when I tried a simple type method an unhandled exception occurred:

> Unhandled Exception: System.EntryPointNotFoundException: Unable to find
> an entry point named IsLetter in DLL Win32DynaLnkLib.dll.
>    at Test.IsLetter(UInt32 c)
>    at Test.Main()

> Can someone suggest how to sort this out or a better way of using C code
> from C# .net.

> Many thanks and regards,

> ..matthew



Fri, 19 Nov 2004 22:31:51 GMT  
 Accessing C code from C#, best way.
Thanks Nick, that works for the char*. I still get the entry point
error, my guess is my dll is not properly created. I think I'll start a
new thread just asking the best (or a simple) way of using C# with C.

Cheers,

..matthew


Quote:

> Matt,

>     The problem stems from the way that strings are represented in C#
> compared to C.  The declaration for your function would be as follows:

> [DllImport("Win32DynaLnkLib.dll")]
> public static extern void ParseConfigFile([MarshalAs(UmanagedType.LPStr)]
> string file);

>     For more information on how to marshal strings to unmanaged code, check
> out the section of the .NET framework documentation titled "Marshaling
> Strings", located at (watch for line wrap):

> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...
> l/cpconmarshalingstrings.asp

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > Hi,

> > I've got the C open source code for a useful program called Tidy which
> > turns malformed html into well formed 'tidied' html or even xml. The
> > Tidy source is quite large so I don't want to port it to C#. But I do
> > need to access its functions from C#.

> > I tried making a dll from Visual C++ V.6 with the C code and then using
> > dllimport in C# like this:

> > [DllImport("Win32DynaLnkLib.dll")]
> > extern void ParseConfigFile(char *file);

> > Adapted from the following which I used to set the system time in .net:

> > [DllImport("kernel32.dll")]
> > static extern bool SetSystemTime(ref SYSTEMTIME time);

> > But the C# compiler first didn't like the 'char *' in ParseConfigFile
> > and when I tried a simple type method an unhandled exception occurred:

> > Unhandled Exception: System.EntryPointNotFoundException: Unable to find
> > an entry point named IsLetter in DLL Win32DynaLnkLib.dll.
> >    at Test.IsLetter(UInt32 c)
> >    at Test.Main()

> > Can someone suggest how to sort this out or a better way of using C code
> > from C# .net.

> > Many thanks and regards,

> > ..matthew



Fri, 19 Nov 2004 23:19:18 GMT  
 Accessing C code from C#, best way.
Matt,

    The error you are getting is for a function named IsError, and yet your
example is for a fnction called ParseConfigFile.  Perhaps you posted the
wrong function in the DLL?

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Thanks Nick, that works for the char*. I still get the entry point
> error, my guess is my dll is not properly created. I think I'll start a
> new thread just asking the best (or a simple) way of using C# with C.

> Cheers,

> ..matthew


> > Matt,

> >     The problem stems from the way that strings are represented in C#
> > compared to C.  The declaration for your function would be as follows:

> > [DllImport("Win32DynaLnkLib.dll")]
> > public static extern void

ParseConfigFile([MarshalAs(UmanagedType.LPStr)]
Quote:
> > string file);

> >     For more information on how to marshal strings to unmanaged code,
check
> > out the section of the .NET framework documentation titled "Marshaling
> > Strings", located at (watch for line wrap):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...

- Show quoted text -

Quote:
> > l/cpconmarshalingstrings.asp

> >     Hope this helps.

> > --
> >                - Nicholas Paldino [.NET MVP]



> > > Hi,

> > > I've got the C open source code for a useful program called Tidy which
> > > turns malformed html into well formed 'tidied' html or even xml. The
> > > Tidy source is quite large so I don't want to port it to C#. But I do
> > > need to access its functions from C#.

> > > I tried making a dll from Visual C++ V.6 with the C code and then
using
> > > dllimport in C# like this:

> > > [DllImport("Win32DynaLnkLib.dll")]
> > > extern void ParseConfigFile(char *file);

> > > Adapted from the following which I used to set the system time in
.net:

> > > [DllImport("kernel32.dll")]
> > > static extern bool SetSystemTime(ref SYSTEMTIME time);

> > > But the C# compiler first didn't like the 'char *' in ParseConfigFile
> > > and when I tried a simple type method an unhandled exception occurred:

> > > Unhandled Exception: System.EntryPointNotFoundException: Unable to
find
> > > an entry point named IsLetter in DLL Win32DynaLnkLib.dll.
> > >    at Test.IsLetter(UInt32 c)
> > >    at Test.Main()

> > > Can someone suggest how to sort this out or a better way of using C
code
> > > from C# .net.

> > > Many thanks and regards,

> > > ..matthew



Fri, 19 Nov 2004 23:57:33 GMT  
 Accessing C code from C#, best way.

Quote:

> Matt,

>  The error you are getting is for a function named IsError, and yet your
> example is for a fnction called ParseConfigFile.  Perhaps you posted the
> wrong function in the DLL?

You mean IsLetter not IsError. I copied a pasted the wrong error message
into my post. I got the same kind of error with ParseConfigFile.

Thanks,

..matthew

Quote:


> > Thanks Nick, that works for the char*. I still get the entry point
> > error, my guess is my dll is not properly created. I think I'll start a
> > new thread just asking the best (or a simple) way of using C# with C.

> > Cheers,

> > ..matthew


> > > Matt,

> > >     The problem stems from the way that strings are represented in C#
> > > compared to C.  The declaration for your function would be as follows:

> > > [DllImport("Win32DynaLnkLib.dll")]
> > > public static extern void
> ParseConfigFile([MarshalAs(UmanagedType.LPStr)]
> > > string file);

> > >     For more information on how to marshal strings to unmanaged code,
> check
> > > out the section of the .NET framework documentation titled "Marshaling
> > > Strings", located at (watch for line wrap):

> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...
> > > l/cpconmarshalingstrings.asp

> > >     Hope this helps.

> > > --
> > >                - Nicholas Paldino [.NET MVP]



> > > > Hi,

> > > > I've got the C open source code for a useful program called Tidy which
> > > > turns malformed html into well formed 'tidied' html or even xml. The
> > > > Tidy source is quite large so I don't want to port it to C#. But I do
> > > > need to access its functions from C#.

> > > > I tried making a dll from Visual C++ V.6 with the C code and then
> using
> > > > dllimport in C# like this:

> > > > [DllImport("Win32DynaLnkLib.dll")]
> > > > extern void ParseConfigFile(char *file);

> > > > Adapted from the following which I used to set the system time in
> .net:

> > > > [DllImport("kernel32.dll")]
> > > > static extern bool SetSystemTime(ref SYSTEMTIME time);

> > > > But the C# compiler first didn't like the 'char *' in ParseConfigFile
> > > > and when I tried a simple type method an unhandled exception occurred:

> > > > Unhandled Exception: System.EntryPointNotFoundException: Unable to
> find
> > > > an entry point named IsLetter in DLL Win32DynaLnkLib.dll.
> > > >    at Test.IsLetter(UInt32 c)
> > > >    at Test.Main()

> > > > Can someone suggest how to sort this out or a better way of using C
> code
> > > > from C# .net.

> > > > Many thanks and regards,

> > > > ..matthew



Sat, 20 Nov 2004 18:58:01 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. Looking for good textbook for CS course

3. Good CS Programs

4. Include code in other Cs files

5. newbe/cs student, need help w/ code

6. Compile CS source code using ICodeCompiler

7. cs source code

8. include another form(cs) in C#?

9. Help with ideas, best ways etc...

10. Good ways to obfuscate/mangle strings?

11. are there better ways to read config file?

12. Any good ways to trace memory leaks?

 

 
Powered by phpBB® Forum Software