Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 24th, 2007, 07:35 PM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mixing C and C# in a project?

I'm working on a project where one library of routines needs to be written in regular C. Not C#, not C++, just C.

The idea is that this file will compile both on the Windows platform (the simulator) and the embedded-systems platform (which I'm not allowed to talk about).

Specifically, I need to make calls to socket() and select() and recv().

I worked with this in a Visual C++ 2005 project and everything worked fine. I didn't do anything special- just stuck to standard C and C calls.


But after some looking at Visual C++ 2005, with all the gyrations needed to work with .NET, we want to investigate using C# as the main language.

Will a C# project accept a .c file? I'm worried that I might be getting into some kind of managed code / unmanaged code warfare.

Thanks.

 
Old March 24th, 2007, 08:25 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

If the C file is compiled you can make calls to the compiled file from within C# but you cannot directly import a .c file.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 24th, 2007, 09:04 PM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much for such a speedy reply.

This is my first .NET program, first C# program, etc.

Would you mind giving me a few more particulars on how I can accomplish making the calls from C# to the C library?

My hunch is that I can either create a .lib file for the C code (can C# use .lib files?) or else I need to create an "assembly" of the C code, which then gets some kind of wrapper and a cautious acceptance on the managed-code side of the fence.

I googled a few different tries, figuring that somebody before me would have tried to mix C and C# before.

Again, thanks for the note.


 
Old March 24th, 2007, 10:44 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I have absolutely no experience using any unmanaged code inside of the runtime. In so far as a lib file, I am not sure if you can include that and makes calls to it, dll's on the other hand I know you can make calls to those.

A word of warning though, when using unmanaged code inside of the runtime, you need to mark the calls as unsafe so something like this:

unsafe
{
//unmanaged calls
}

This article should give you some futher insight:

http://www.codersource.net/csharp_unsafe_code.html

let me know if you have any more questions.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 25th, 2007, 02:40 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I may have taken us off track with my last posting.

Frankly, I don't know if what I'm trying to do has anything to do with unmanaged code. But I do know that the file can only be compiled with the C++ compiler, not the C# compiler.

And I apologize if I'm asking too much of a C/C++ question in a C# forum.

One of the things that's mentioned in every VS2005 book that I've seen is "language interoperability" -- the idea that, because of the common IL and CLR, it's easy to build an application using VB for the GUI and VC# for the backend. Or similar mixing of languages.

Yet, I don't see any way to include source files from multiple languages in a single project.



And (I think) that's what I want to do. I want to write most of my code in C#. The one "common file" that has to be compiled both under Windows and in the embedded system won't compile for C#. It WILL, however, compile for a C++ console program.

Sadly, I can't tell if this C++ program is being built as managed code or unmanaged code. I don't fool around with pointers, for example. And it compiles cleanly with the /CLR flag.

But the syntax is C++, not C#. The include files are for C++, not C#. So C++ compiles it without a warning, whereas C# won't compile it at all.

And it makes extensive use of what I call the C runtime libraries. It calls socket() and select() rather than System.IO.Socket() [sp].

So it uses WinSock, rather than the .NET implementation of sockets.

And it WORKS, compiled as part of a command-line application for C++.

But I need it to be part of a Windows Forms-based application in C#.

You had mentioned the possibility of making calls to the compiled C / C++ file from C#. Can you add some detail on the mechanism for doing that?

I can't tell you how grateful I am for your insights. I've been programming for 30 years, and it's very frustrating being tripped up by the language mechanics of getting things done. I did some Windows programming before .NET came along, then switched to embedded programming (done in C) and I'm finding it difficult to marry the two.


 
Old March 26th, 2007, 06:27 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Ok, I have been thinking about this all weekend.

I am unfamiliar with the /CLR switch, however, I assume that the source files are compiled and a CLR header is added to the assembly, in which case that code will run as managed code. (One way to test this would be to compile your C files and then try opening that assembly using ILDASM.exe. If your code has been compiled as managed code ILDASM will be able to disassemble it, else it wont.)

So, for the sake of argument, lets say that you are able to compile your C code down into managed code. (In this case you can ignore my post about the unsafe keyword.)

Essentially what you will need to do, create an assembly of your C code from a project (I am pretty sure it needs to be compiled down into a dll as opposed to a lib file, but I could be wrong because I have never tried to use a lib file as a reference in a project)

After you have created your C assembly open up your C# project and in the Solution Explorer right click on references and select 'Add Reference' then navigate to the location of your C assembly.

Since this is managed code you shouldn't have any problems making calls to it and you would instantiate it like any other object.

MyNameSpace.Foo someObject = new MyNameSpace.Foo();
someObject.someMethod();

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 26th, 2007, 06:57 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, again, thank you for your time and advice.

I got the thing working, oh, pretty late last night.

The solution is to create a C++ Class Library project. And I believe that the .dll file is indeed what the C# reference adder wanted.

I know that my code is being compiled with /clr, but I don't think that makes it "managed" code automatically. (And I'm not sure of the distinction between "unmanaged" code and "unsafe" code. Not yet.)

My code uses pointers, so I'm sure it's not considered "safe" - but after adding the reference I didn't have to do anything special in terms of defining my code as unsafe.

Eventually I have to figure out how to pass my C++ class a "callback" object. See, in my C++ class library, there doesn't seem to be any way to get to MessageBox.Show(). (Actually, the definition requires it to use a supplied function pointer for writing log entries.)

Now, I have learned (and thank you) how to get a C++ class library (with code) integrated into a C# project. But I don't know how to pass a C# object to the C++ class library so the C++ class library can invoke the methods of the C# object -- callbacks. I figure I can just pass the object in by reference, and tell the C++ program that it has a myObject ^ pointer. But I don't know how to tell the C++ the interface for myObject -- what methods can be called and so on.

But once I get there, it'll be all solved.

I found an example in VB, using the "interface" and "implements" keywords, but I haven't been able to find one in C++.

Many thanks.


 
Old March 26th, 2007, 07:05 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Glad you got everything working ^^ Unmanaged code is any code that does not run within the .NET framework (A VB 5.0 application would be an example of unmanaged code) and the unsafe keyword is normally applied when using pointers (ala managing memory directly)

http://msdn2.microsoft.com/en-us/library/t2yzs44b.aspx

If you need anymore help let me know.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixing PHP and Javascript nmjnmj Pro PHP 4 December 9th, 2006 01:31 AM
Session Id mixing jejedp VS.NET 2002/2003 0 January 4th, 2006 07:54 PM
mixing results of checkboxes Hatchingabrain Javascript How-To 8 April 6th, 2005 08:37 AM
mixing results of checkboxes Hatchingabrain Javascript 2 April 4th, 2005 04:02 AM
Mixing Scripting Languages digriz60 ASP.NET 1.0 and 1.1 Basics 4 October 27th, 2003 02:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.