Access C# method from a C++ project located in the same solution

Madi Abdou Ramanou 1 Reputation point

I am building a solution that contains two projects (one is C# class library and one other a C++ static library). I added the C# project to the C++ project as reference and also i checked the register as interop COM and make Assembly COM-Visible checkbox in C# project. But i ca't see the neither namespace nor the class or methods of C# project in the C++ project.

Please help me

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,562 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Madi Abdou Ramanou 1 Reputation point


    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Media.Imaging;
    using System.Runtime.InteropServices;
    using WIA;
    namespace MyLib1
    public class MySharpClass1
    const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";

        public static string convertBitmapToBase64(BitmapImage bi)
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            byte[] bitmapdata = ms.ToArray();
            return Convert.ToBase64String(bitmapdata);
        public static string convBmpToBase64Strings(Bitmap bmp, ImageFormat imageFormat)
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = stream.ToArray();
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        void SharpScanDocument()
            DeviceManager deviceManager;
            DeviceInfos devices;
            deviceManager = new DeviceManager();
            devices = deviceManager.DeviceInfos;
            DeviceInfo selectedDevice;
            Device ourdevice;
            int selectedScanIndex = 0;
            string base64String = "";
            for (int i = 1; i <= devices.Count; i++)
                DeviceInfo device;
                device = devices[i];
                if (device.Type == WiaDeviceType.ScannerDeviceType)
                    selectedScanIndex = i;
            if (selectedScanIndex > 0)
                selectedDevice = devices[selectedScanIndex];
                ourdevice = selectedDevice.Connect();
                Items items;
                items = ourdevice.Items;
                WIA.Item item = ourdevice.Items[1] as WIA.Item;
                WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                WIA.ImageFile img = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);
                byte[] buffer = (byte[])img.FileData.get_BinaryData();
                MemoryStream ms = new MemoryStream(buffer);
                Bitmap bmp = new Bitmap(Image.FromStream(ms));
                base64String = convBmpToBase64Strings(bmp, bmp.RawFormat);
            Console.WriteLine($"Scan done");



    include "pch.h"

    include "CppLib1.h"

    namespace MyCppSpace {
    class CppLib1 {
    void testSharpCall() {

  2. Jeanine Zhang-MSFT 9,266 Reputation points Microsoft Vendor


    But i ca't see the neither namespace nor the class or methods of C# project in the C++ project.

    I suggest you could refer to the answer:

    COM itself does not have a concept of namespace (which is language-specific).

    RegAsm.exe uses the assembly name as the name of the library statement in the IDL. It does not care which namespace your COMVisible class is in.

    By default the C++ compiler imports a type library into a namespace with its name specified by the library statement in the IDL. Use the assembly name as the namespace name (or rename the namespace in the import statement) in your C++ code.

    If you want to use COM. As far as I'm concerned you should make your C# class exposed to COM and then you can use it in C++. For more details I suggest you could refer to the thread:

    Best Regards,


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.