Calling C# method in C

Asked

Viewed 60 times

2

I am trying to import a DLL generated via C# in C.

I’m using the methods LoadLibrary to load the DLL, but when using the GetProcAddress the return is always null. I used the GetLastError and the error code is 127, which means "Function not found".

Code C#:

using RGiesecke.DllExport;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Testando
{
    [ComVisible(true)]
    [Guid("5295bba9-fd9b-474d-bd6a-2f7eef51ab71")]
    public class Teste1
    {
        public int LaVai()
        {
            return 1;
        }
    }
}

Code C:

HMODULE hModule = LoadLibrary("Testando.dll");


if(hModule == NULL){
    printf("null");
}else{
    printf("not null");
}

_TestFunc = (TestFunc) GetProcAddress(hModule, TEXT("LaVai"));  

if(_TestFunc == NULL){
    printf("null");
    printf("%d",GetLastError());
}else{
    printf("not null");
}

return 0;

What is missing?

1 answer

1

Check the following conditions:

1. Estate Register for COM interop in the separator Build of the project

inserir a descrição da imagem aqui

2. Information of COM Visible and unique GUID in class AssemblyInfo.cs which is in Properties (GUID):

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("89ff3859-a555-4dbe-8198-c9e74d2b38c1")]

Compile the project again and then try to access the method you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.