这是我在VC里面写的一段代码,可以正确地得到结果,可把这段代码实现的功能移植到C#里面,总是不能成功,谁能帮我移植成功,200分重谢。#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <string.h>//#include <Imm.h>
using namespace std;int main(int argc, CHAR* argv[])
{
/*char test[80];
GetKeyBoardLayout(test);*/ STYLEBUF stlBuff[1];


HKL hKL = LoadKeyboardLayout("E0010411", KLF_ACTIVATE);

// 得到style的个数
int num = ImmGetRegisterWordStyle(hKL, 0, stlBuff); // 得到所有的style
STYLEBUF *stlBuff1 = new STYLEBUF[num];
int rc = ImmGetRegisterWordStyle(hKL, num, stlBuff1);

}

解决方案 »

  1.   

    谢谢楼上的
    LoadKeyboardLayout
    ImmGetRegisterWordStyle
    是两个api函数
      

  2.   

    是不是你的api声明有问题啊?http://tech.163.com/04/1120/14/15KV1EO00009rt.html
    看看这个
      

  3.   

    c++里面有很多define的东西,HKL,STYLEBUF什么的,代表什么?
      

  4.   

    我觉得你最好还是将这个封装成一个dll,然后在C#里面调用那个函数,这样转,一些特殊的类型很难转化的
      

  5.   

    用编码Sytem.text命名空间里面的东东:Encoding
      

  6.   

    ImmGetRegisterWordStyle 找不到替代函数,已经得到键盘布局句柄了。查了半天不知道怎么把VC里的imm.h文件导入C#里。
      

  7.   

    C#中声明import调用Api,如果还不行就用VC写个Com件,由C#引入调用,也可以写个普通的动态链接库暴露出接口再由C# 用import申明后使用
      

  8.   

    typedef HANDLE HKL;
    typedef PVOID HANDLE; 
    typedef void *PVOID; //指向任何类型的指针。
    typedef unsigned long DWORD;
    typedef struct _tagSTYLEBUF {
      DWORD  dwStyle;           
      TCHAR  szDescription[STYLE_DESCRIPTION_SIZE];  
    } STYLEBUF, *PSTYLEBUF;
    这是个结构,转为c#:
    struct _tagSTYLEBUF
    {
       ulong dwStyle;
       short[STYLE_DESCRIPTION_SIZE] szDescription;
    }对于api函数需要属于dllimport来导入
    HKL LoadKeyboardLayout(          LPCTSTR pwszKLID,
        UINT Flags
    );
    --------->>
    [DLLImport("user32.dll", EntryPoint="LoadKeyboardLayout" CharSet=CharacterSet.Ansi)]
     public static extern int LoadKeyboardLayout(string pwszKLID,uint Flags);其它的类似。转成c#的要费很多事……
      

  9.   

    首先声明一个结构:
    public struct STYLEBUF
    {
    Int32 dwStyle;
    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]    
    String szDescription;
    }
      

  10.   

    算了,还是复制整个Form的代码给你吧.看着省事些,免得到时候搞出其他问题。using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace test_Winform
    {
    /// <summary>
    /// Form3 的摘要说明。
    /// </summary>
    public class Form3 : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form3()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form3
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form3";
    this.Text = "Form3";
    this.Load += new System.EventHandler(this.Form3_Load); }
    #endregion [ DllImport( "user32.dll" )]
    public static extern IntPtr LoadKeyboardLayout( String pwszKLID, UInt32 Flags );
    [ DllImport( "imm32.dll" )]
    public static extern UInt32 ImmGetRegisterWordStyle( IntPtr hKL, UInt32 nItem, [In, Out] STYLEBUF[] lpStyleBuf ); private void Form3_Load(object sender, System.EventArgs e)
    {
    STYLEBUF[] stlBuff = new STYLEBUF[1];
    IntPtr hKL = LoadKeyboardLayout("E0010411", 1); // 得到style的个数
    UInt32 num = ImmGetRegisterWordStyle(hKL, 0, stlBuff); // 得到所有的style
    STYLEBUF[] stlBuff1 = new STYLEBUF[num];
    UInt32 rc = ImmGetRegisterWordStyle(hKL, num, stlBuff1);
    }
    } public struct STYLEBUF
    {
    Int32 dwStyle;
    [ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]    
    String szDescription;
    }
    }
      

  11.   

    华君兄,I 服了 you,看样子你是既精通VC,又精通C#,非常感谢,谢谢!!!
      

  12.   


    华君兄,不过还有个问题向你请教,[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]这一句是什么含义,什么情况下用???
      

  13.   

    具体请参考.NET Framework 类库中的UnmanagedType 枚举。