Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to call C API in .lib file from C#. Pls let me know whether this can be done.

following is the code snippet for C Lib.
XML
#include "stdafx.h"

#include <stdio.h>

void print(const char *message)
{
       printf("%s\\n", message);
}



I am calling this function from c# but it is crashing
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CSharp_CLib
{
    class Class1
    {
        [DllImport("C_API_APP.lib", EntryPoint = "print")]

        static extern void print(string message);

        public static void Main(string[] args)
        {

            print("Hello World C# => C++");
        }
    }
}
Posted
Comments
joshrduncan2012 21-Aug-13 13:24pm    
Why are you mixing languages?
SNI 21-Aug-13 13:35pm    
I have libs in C which I used it with MFC (VS2010) application. But due to some limitation in MFC I want to check whether I can use these C APIs from C# so I do not need to convert these libs into dll and then use as an interop. pls suggest.....

You cannot call functions of a static library, you have to create a dynamic library (DLL) for the purpose.
See, for instance "Using C++ Static Libraries in C#" at CodeGuru[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-13 14:43pm    
Agree, a 5. I added detail about two options: using P/Invoke or C++/CLI, please see my answer.
—SA
fjdiewornncalwe 21-Aug-13 16:57pm    
Absolutely +5.
CPallini 21-Aug-13 17:29pm    
Thank you.
You really need to create a C DLL and use it via P/Invoke: http://en.wikipedia.org/wiki/P/Invoke[^].

Please see: http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article will also be useful: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

Another option is using, instead of C, not even C++, but C++/CLI. Usually, you can put your C code in such project. The project should be mixed-mode: managed+unmanaged. This way, you can use C code but wrap it in some C++/CLI "ref" types to expose them as you usually do in a regular .NET assembly. Then you can reference this assembly in any .NET project. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA
 
Share this answer
 
v2
Comments
CPallini 21-Aug-13 15:02pm    
5.
Sergey Alexandrovich Kryukov 21-Aug-13 18:43pm    
Thank you, Carlo.
—SA
Sergey Alexandrovich Kryukov 21-Aug-13 16:43pm    
Thank you, Carlo.
—SA
fjdiewornncalwe 21-Aug-13 16:57pm    
5 as well.
Sergey Alexandrovich Kryukov 21-Aug-13 18:43pm    
Thank you, Marcus.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900