有一个ocx的控件,是用C++做成的,封装成了COM接口。
其中的一个方法是 void GetDiNum(VARIANT* diList);
在控件中被封装成了 void GetDiNum(ref object diList);
在ocx中 void GetDiNum(VARIANT* diList)方法中是用COleSafeArray来实现的。
下面是部分代码:
void GetDiNum(VARIANT* diList)
{
......
COleSafeArray sa;
sa.vt = VT_ARRAY | VT_BSTR;
sa.parray = diList->parray;
sa.ResizeOneDim( 0 );
}
Importing SafeArrayCreate (i.e., [DllImport("oleaut32.dll")] SafeArrayCreate) ;
.net framework 2.0 以上版本中可以使用variantwrapper
string[] tempstr = new string[1];
object var = new System.Runtime.InteropServices.VariantWrapper(tempstr);
ret = axKcat1.GetDiNum(ref var);
得到object类型的var,将其转换成((string[])var)就可以按照字符串数组来使用了。
巧用Marshal.GetDelegateForFunctionPointer--C#如何调用按键精灵插件dll
Posted on 2008-02-10 15:20 绿叶 阅读(1878) 评论(8) 编辑 收藏
原来是为了在游戏外挂中发送键盘鼠标消息,自己写个sendmessage或者是postmessage又比较麻烦。于是google了一下,发现现在很多脚本工具都有这个功能,其中按键精灵的一个叫361度的插件已经有这个的实现,还验证过了。为什么不拿来己用呢?
首先分析一下按键精灵插件的接口,发现:
插件的功能函数没有直接暴露出来,而是通过一个GetCommand的函数返回一个函数描述结构。
接下来看看这个结构:
上面这个结构我已经是转换成C#的对应结构了,原结构可以查看按键精灵提供的插件C++接口源代码。
这个结构里面的 handlerFunction 实际上是指向函数的入口点,也就是一个函数指针,每个函数都一样是2个参数:
typedef int (*QMPLUGIN_HANDLER)(char *lpszParamList, char *lpszRetVal);
转换为C#中相应的委托为:
delegate void Invoker(string parameters, StringBuilder returnValue);
大家注意到,有两个参数,c++原型中都是char*类型,转换为C#的delegate后第一个为string,第二个为StringBuilder。这是因为parameters是in的,dll中不会对这个参数做修改,而returnValue是out的,dll返回时候要把返回值写入这个StringBuilder的缓冲区。
原本的想法是用C++写一个桥来调用dll,不过在.net 2.0 中,框架直接提供了 Marshal.GetDelegateForFunctionPointer 来转换一个函数指针为一个委托,这就方便多拉。请看下面代码,注意看 BGKM_ExecuteCommand 这个函数里面的东西。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WJsHome.Game.Utility
{
public class QMacro
{
[DllImport("BGKM5.dll", EntryPoint = "GetCommand")]
static extern IntPtr BGKM_GetCommand(int commandNum);
[StructLayout(LayoutKind.Sequential)]
class QMPLUGIN_CMD_INFO
{
public string commandName;
public string commandDescription;
public IntPtr handlerFunction;
public uint paramNumber;
}
delegate void Invoker(string parameters, StringBuilder returnValue);
static string BuildParameters(params object[] parameters)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < parameters.Length; i++)
{
sb.Append(parameters[i].ToString());
if (i != parameters.Length - 1)
{
sb.Append(',');
}
}
return sb.ToString();
}
static void BGKM_ExecuteCommand(int cmdNum, string parameters, StringBuilder retVal)
{
IntPtr pCmdInfo = BGKM_GetCommand(cmdNum);
QMPLUGIN_CMD_INFO cmdInfo = new QMPLUGIN_CMD_INFO();
Marshal.PtrToStructure(pCmdInfo, cmdInfo);
Invoker invoker = Marshal.GetDelegateForFunctionPointer(cmdInfo.handlerFunction, typeof(Invoker)) as Invoker;
invoker(parameters, retVal);
}
public static void BGKM_KeyClick(IntPtr hWnd, int key)
{
BGKM_ExecuteCommand(0, BuildParameters(hWnd, key), null);
}
public static void BGKM_KeyDown(IntPtr hWnd, int key)
{
BGKM_ExecuteCommand(1, BuildParameters(hWnd, key), null);
}
public static void BGKM_Mouse(IntPtr hWnd, int code, int x, int y)
{
BGKM_ExecuteCommand(15, BuildParameters(hWnd, code, x, y), null);
}
public static WinApi.POINT BGKM_ScrToCli(IntPtr hWnd, int x, int y)
{
StringBuilder retVal = new StringBuilder();
BGKM_ExecuteCommand(16, BuildParameters(hWnd, x, y), retVal);
string[] tmp = retVal.ToString().Split('|');
return new WinApi.POINT(int.Parse(tmp[0]), int.Parse(tmp[1]));
}
}
}
--------------------------------------------------------------------------------
好了,方便哇?这样一来,我们可以在.net上面实现动态加载和卸载Win32 dll. 具体思路就是:(还是代码来得方便)
public delegate int MsgBox(int hwnd,string msg,string cpp,int ok);
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
private static Delegate GetAddress(int dllModule, string functionname, Type t)
{
int addr = GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
}
private void button1_Click(object sender, EventArgs e)
{
int huser32 = 0;
huser32 = LoadLibrary("user32.dll");
MsgBox mymsg = (MsgBox)GetAddress(huser32, "MessageBoxA", typeof(MsgBox));
mymsg(this.Handle.ToInt32(), txtmsg.Text, txttitle.Text , 64);
FreeLibrary(huser32);
}上面代码是从internet上copy下来的,anyway, enjoy
请问C#如何调用API、DLL文件?
此帖子是否有帮助?
一个PlaySound示例:
程序块using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace ChessGame
{
/// <summary>
/// 用于播放音乐
/// </summary>
internal class Helpers
{
[Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000, /* play synchronously (default) */ //同步
SND_ASYNC = 0x0001, /* play asynchronously */ //异步
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
[DllImport("winmm")]
public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );
}
public class Sound
{
public static void Play( string strFileName )
{
//Helpers.PlaySound( strFileName, IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME | Helpers.PlaySoundFlags.SND_ASYNC );
switch(strFileName)
{
case "start": strFileName=@"../../sound/start.WAV"; break;
case "back": strFileName=@"../../sound/back.WAV"; break;
case "fall": strFileName=@"../../sound/fall.WAV"; break;
case "huiqi": strFileName=@"../../sound/huiqi.WAV"; break;
case "huiqiend": strFileName=@"../../sound/huiqiend.WAV"; break;
case "jiangjun": strFileName=@"../../sound/jiangjun.WAV"; break;
case "kill": strFileName=@"../../sound/kill.WAV"; break;
case "win": strFileName=@"../../sound/win.WAV"; break;
case "move": strFileName=@"../../sound/move.WAV"; break;
case "hold": strFileName=@"../../sound/hold.WAV"; break;
case "no": strFileName=@"../../sound/no.WAV"; break;
case "popup": strFileName=@"../../sound/popup.WAV"; break;
case "mayfall": strFileName=@"../../sound/mayfall.WAV"; break;
case "return_red": strFileName=@"../../sound/return_red.WAV"; break;
case "return_blue": strFileName=@"../../sound/return_blue.WAV";break;
}
Helpers.PlaySound(strFileName, IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME | Helpers.PlaySoundFlags.SND_ASYNC);
}
}
}
基于C#的接口基础教程之四
第四节、访问接口
对接口成员的访问
对接口方法的调用和采用索引指示器访问的规则与类中的情况也是相同的。如果底层成员的命名与继承而来的高层成员一致,那么底层成员将覆盖同名的高层成员。但由于接口支持多继承,在多继承中,如果两个父接口含有同名的成员,这就产生了二义性(这也正是C#中取消了类的多继承机制的原因之一),这时需要进行显式的定义:
using System ;
interface ISequence {
int Count { get; set; }
}
interface IRing {
void Count(int i) ;
}
interface IRingSequence: ISequence, IRing { }
class CTest {
void Test(IRingSequence rs) {
//rs.Count(1) ; 错误, Count 有二义性
//rs.Count = 1; 错误, Count 有二义性
((ISequence)rs).Count = 1; // 正确
((IRing)rs).Count(1) ; // 正确调用IRing.Count
}
}
上面的例子中,前两条语句rs .Count(1)和rs .Count = 1会产生二义性,从而导致编译时错误,因此必须显式地给rs 指派父接口类型,这种指派在运行时不会带来额外的开销。
再看下面的例子:
using System ;
interface IInteger {
void Add(int i) ;
}
interface IDouble {
void Add(double d) ;
}
interface INumber: IInteger, IDouble {}
class CMyTest {
void Test(INumber Num) {
// Num.Add(1) ; 错误
Num.Add(1.0) ; // 正确
((IInteger)n).Add(1) ; // 正确
((IDouble)n).Add(1) ; // 正确
}
}
调用Num.Add(1) 会导致二义性,因为候选的重载方法的参数类型均适用。但是,调用Num.Add(1.0) 是允许的,因为1.0 是浮点数参数类型与方法IInteger.Add()的参数类型不一致,这时只有IDouble.Add 才是适用的。不过只要加入了显式的指派,就决不会产生二义性。
接口的多重继承的问题也会带来成员访问上的问题。例如:
interface IBase {
void FWay(int i) ;
}
interface ILeft: IBase {
new void FWay (int i) ;
}
interface IRight: IBase
{ void G( ) ; }
interface IDerived: ILeft, IRight { }
class CTest {
void Test(IDerived d) {
d. FWay (1) ; // 调用ILeft. FWay
((IBase)d). FWay (1) ; // 调用IBase. FWay
((ILeft)d). FWay (1) ; // 调用ILeft. FWay
((IRight)d). FWay (1) ; // 调用IBase. FWay
}
}
public class EditBox: IControl, IDataBound {
void IControl.Paint( ) {...}
void IDataBound.Bind(Binder b) {...}
}
因为通过外部指派接口成员实现了每个成员,所以用这种方法实现的成员称为外部接口成员。外部接口成员可以只是通过接口来调用。例如,Paint方法中EditBox的实现可以只是通过创建Icontrol接口来调用。
class Test {
static void Main( ) {
EditBox editbox = new EditBox( );
editbox.Paint( ); //错误: EditBox 没有Paint 事件
IControl control = editbox;
control.Paint( ); // 调用 EditBox的Paint事件
}
}
上例中,类EditBox 从Control 类继承并同时实现了IControl and IDataBound 接口。EditBox 中的Paint 方法来自IControl 接口,Bind 方法来自IDataBound 接口,二者在EditBox 类中都作为公有成员实现。当然,在C# 中我们也可以选择不作为公有成员实现接口。
如果每个成员都明显地指出了被实现的接口,通过这种途径被实现的接口我们称之为显式接口成员(explicit interface member)。 用这种方式我们改写上面的例子:
public class EditBox: IControl, IDataBound {
void IControl.Paint( ) {…}
void IDataBound.Bind(Binder b) {…}
}
显式接口成员只能通过接口调用。例如:
class CTest {
static void Main( ) {
EditBox editbox = new EditBox( ) ;
editbox.Paint( ) ; //错误:不同的方法
IControl control = editbox;
control.Paint( ) ; //调用 EditBox的Paint方法
}
}
上述代码中对editbox.Paint( )的调用是错误的,因为editbox 本身并没有提供这一方法。control.Paint( )是正确的调用方式。
注释:接口本身不提供所定义的成员的实现,它仅仅说明这些成员,这些成员必须依靠实现接口的类或其它接口的支持。
知道了怎样访问接口,我们还要知道怎样实现接口,要实现C#的接口,请看下一节-实现接口
上例中,方法IBase.FWay在派生的接口ILeft中被Ileft的成员方法FWay覆盖了。所以对d. FWay (1)的调用实际上调用了。虽然从IBase-> IRight-> IDerived这条继承路径上来看,ILeft.FWay方法是没有被覆盖的。我们只要记住这一点:一旦成员被覆盖以后,所有对其的访问都被覆盖以后的成员"拦截"了。
类对接口的实现
前面我们已经说过,接口定义不包括方法的实现部分。接口可以通过类或结构来实现。我们主要讲述通过类来实现接口。用类来实现接口时,接口的名称必须包含在类定义中的基类列表中。
下面的例子给出了由类来实现接口的例子。其中ISequence 为一个队列接口,提供了向队列尾部添加对象的成员方法Add( ),IRing 为一个循环表接口,提供了向环中插入对象的方法Insert(object obj),方法返回插入的位置。类RingSquence 实现了接口ISequence 和接口IRing。
using System ;
interface ISequence {
object Add( ) ;
}
interface ISequence {
object Add( ) ;
}
interface IRing {
int Insert(object obj) ;
}
class RingSequence: ISequence, IRing
{
public object Add( ) {…}
public int Insert(object obj) {…}
}
如果类实现了某个接口,类也隐式地继承了该接口的所有父接口,不管这些父接口有没有在类定义的基类表中列出。看下面的例子:
using System ;
interface IControl {
void Paint( );
}
interface ITextBox: IControl {
void SetText(string text);
}
interface IListBox: IControl {
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox { }
这里, 接口IcomboBox继承了ItextBox和IlistBox。类TextBox不仅实现了接口ITextBox,还实现了接口ITextBox 的父接口IControl。
前面我们已经看到,一个类可以实现多个接口。再看下面的例子:
interface IDataBound {
void Bind(Binder b);
}
public class EditBox: Control, IControl, IDataBound {
public void Paint( );
public void Bind(Binder b) {...}
}
类EditBox从类Control中派生并且实现了Icontrol和IdataBound。在前面的例子中接口Icontrol中的Paint方法和IdataBound接口中的Bind方法都用类EditBox中的公共成员实现。C#提供一种实现这些方法的可选择的途径,这样可以使执行这些的类避免把这些成员设定为公共的。接口成员可以用有效的名称来实现。例如,类EditBox可以改作方法Icontrol.Paint和IdataBound.Bind来来实现。