简体   繁体   中英

C# call C++ DLL through function pointer

#ifndef HEADER_INFOAPI
#define HEADER_INFOAPI

#define EXTERN_C extern "C"
#define INFOAPI __declspec(dllexport)


#include <windows.h>
#include <TCHAR.h>

struct Info
{
    virtual void StartPs() = 0;
    virtual double GetHt() = 0;
    virtual bool IsShowInfo() = 0;
};

typedef Info* INFOHANDLE;

EXTERN_C INFOAPI INFOHANDLE WINAPI GetInfo(wchar_t* File1, 
                                                                wchar_t* File2, 
                                                                double Height, 
                                                                int Num);


#endif

I have written a DLL in dev C++. The DLL's name is "Info.dll" and it contains one functions: "GetInfo". The header file looks like this:

How do I call it in C# ?

hi, c45207

I tried what you said and got a wrong value of bodyRes.GetHt()

In my expect the bodyRes.GetHt() should be = Double test = 170.0,

but I got 9.2079039212996476E-275

Can you kindly help me to check out if anything that I did wrong?

============================= code ===============================

Double test = 170.0;

IntPtr x = GetInfo("C:\\t.jpg", "C:\\e.jpg", test, 0);

IBodyInfo bodyRes = (Info)Marshal.PtrToStructure(x, typeof(Info));

bodyRes.StartPs();

====================================================================

============================= result =============================

bodyRes.GetHt()         9.2079039212996476E-275     double
bodyRes.IsShowInfo()    false                       bool

====================================================================

You tagged the question with DllImport, so I assume you want to use that/PInvoke. Does something like this work for you? If not, clarify the question, please.

using System.Runtime.InteropServices;

[DllImport("Info.dll", CharSet=CharSet.Unicode)]
public static extern IntPtr GetInfo(string File1, string File2, double Height, int Num);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM