在C#应用程序中使用VC ++代码 [英] Using VC++ code in c# Application

查看:101
本文介绍了在C#应用程序中使用VC ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VC ++中创建了一个应用程序,该应用程序根据Text创建位图数据.
并且我已经从VC ++ dll中导出了该函数.

在C#中,我正在使用DllImport
访问此功能 我想通过C#将LOGFONT结构作为参数传递给VC ++应用程序.
我该如何通过?

请参考代码示例
VC ++

I have created one application in VC++ which creates bitmap data according to Text
and I have exported the function from VC++ dll.

In C#, i am accessing this function by using DllImport
I want to pass LOGFONT structure as a parameter to VC++ application through C#.

How can i pass this?

Please Refer to Code sample
VC++

Exported Function looks like this
void MyDllMethod(BYTE *pbByteArray,char* a_sMessage,int a_eHorizontalAllignment, bool a_bDrawVertical,LOGFONT m_winLogFont);



c#
LOGFONT结构定义为



c#
LOGFONT structure is defined as

public struct LOGFONT
    {
        public int lfHeight;
        public int lfWidth;
        public int lfEscapement;
        public int lfOrientation;
        public int lfWeight;
        public byte lfItalic;
        public byte lfUnderline;
        public byte lfStrikeOut;
        public byte lfCharSet;
        public byte lfOutPrecision;
        public byte lfClipPrecision;
        public byte lfQuality;
        public byte lfPitchAndFamily;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string lfFaceName;

        public LOGFONT(bool test)
        {
            lfHeight = 0;
            lfWidth = 0;
            lfEscapement = 0;
            lfOrientation = 0;
            lfWeight = 0;
            lfItalic = 0;
            lfUnderline = 0;
            lfStrikeOut = 0;
            lfCharSet = 0;
            lfOutPrecision = 0;
            lfClipPrecision = 0;
            lfQuality = 0;
            lfPitchAndFamily = 0;
            lfFaceName = "Arial";
        }
    }

[DllImport("Bitmap Creation.dll", EntryPoint = "MyDllMethod", ExactSpelling = false,CallingConvention = CallingConvention.Cdecl)]

public static extern void MyDllMethod(IntPtrbyteArray,                                               [MarshalAs(UnmanagedType.LPStr)]string message,
           int a_eHorizontalAllignment, bool a_bDrawVertical,LOGFONT a_logFont);


但是在VC ++应用程序中,我在LOGFONT结构中得到了一些垃圾值.
请帮助.....等待您的答复...


But in VC++ application, I am getting some garbaouge values in LOGFONT structure.
Please Help..... waiting for your reply...

推荐答案

已尝试放置一个类而不是"Enum":
Have your tryed to put a class instead of "Enum" :
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
       public class LOGFONT



看看这个链接,可能对您有帮助
http://www.pinvoke.net/default.aspx/Structures/LOGFONT.html [ ^ ]



Take a look at this link, may be it can help you
http://www.pinvoke.net/default.aspx/Structures/LOGFONT.html[^]


我认为您需要使用 StructLayoutAttribute [ ^ ]类,确保LOGFONT元素的位置与C ++结构完全相同.
I think you need to use the StructLayoutAttribute[^] class, to ensure the LOGFONT elements are positioned exactly the same as the C++ structure.


这篇关于在C#应用程序中使用VC ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆