这是我在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);
}
#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);
}
LoadKeyboardLayout
ImmGetRegisterWordStyle
是两个api函数
看看这个
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#的要费很多事……
public struct STYLEBUF
{
Int32 dwStyle;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]
String szDescription;
}
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;
}
}
华君兄,不过还有个问题向你请教,[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=32 )]这一句是什么含义,什么情况下用???