Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:16:04+00:00 2026-06-07T10:16:04+00:00

I have a C lib and want to call function in this library from

  • 0

I have a C lib and want to call function in this library from C# application. I tried creating a C++/CLI wrapper on the C lib by adding the C lib file as linker input and adding the source files as additional dependencies.

Is there any better way to achieve this as am not sure how to add C output to c# application.

My C Code –

__declspec(dllexport) unsigned long ConnectSession(unsigned long handle,
                            unsigned char * publicKey,
                            unsigned char   publicKeyLen);

My CPP Wrapper –

long MyClass::ConnectSessionWrapper(unsigned long handle,
                                unsigned char * publicKey,
                                unsigned char   publicKeyLen)
    {
        return ConnectSession(handle, publicKey, publicKeyLen);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:16:05+00:00Added an answer on June 7, 2026 at 10:16 am

    The example will be, for Linux:

    1) Create a C file, libtest.c with this content:

    #include <stdio.h>
    
    void print(const char *message)
    {
      printf("%s\\n", message);
    }
    

    That’s a simple pseudo-wrapper for printf. But represents any C function in the library you want to call. If you have a C++ function don’t forget to put extern C to avoid mangling the name.

    2) create the C# file

    using System;
    
    using System.Runtime.InteropServices;
    
    public class Tester
    {
            [DllImport("libtest.so", EntryPoint="print")]
    
            static extern void print(string message);
    
            public static void Main(string[] args)
            {
    
                    print("Hello World C# => C++");
            }
    }
    

    3) Unless you have the library libtest.so in a standard library path like “/usr/lib”, you are likely to see a System.DllNotFoundException, to fix this you can move your libtest.so to /usr/lib, or better yet, just add your CWD to the library path: export LD_LIBRARY_PATH=pwd

    credits from here

    EDIT

    For Windows, it’s not much different.
    Taking an example from here, you only have yo enclose in your *.cpp file your method with extern "C"
    Something like

    extern "C"
    {
    //Note: must use __declspec(dllexport) to make (export) methods as 'public'
          __declspec(dllexport) void DoSomethingInC(unsigned short int ExampleParam, unsigned char AnotherExampleParam)
          {
                printf("You called method DoSomethingInC(), You passed in %d and %c\n\r", ExampleParam, AnotherExampleParam);
          }
    }//End 'extern "C"' to prevent name mangling
    

    then, compile, and in your C# file do

    [DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]
    
    public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
    

    and then just use it:

    using System;
    
        using System.Runtime.InteropServices;
    
        public class Tester
        {
                [DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]
    
        public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);
    
                public static void Main(string[] args)
                {
                        ushort var1 = 2;
                        char var2 = '';  
                        DoSomethingInC(var1, var2);
                }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a generate(variable) function in another file, i want to call it in
I have a jar file that I want to put in the shared lib
I have .lib file with its header (.h) file. This file have a few
I have C++ source code (xyz.cpp) for static library called libxyz.lib, in this xyz.cpp
From this tutorial . see i have one pre-built static library named as stackoverflow.a
I have a class Test which is in lib folder and I want to
I have a lib/languages.py file only with a dict format e.g.: en = {}
I have downloaded ffmpeg lib file and complied it for armv7. I added ffmpeg
We have a library created as both .lib and .dll (it's a big library
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.