菜鸟第一次用需要详细指导,自己对应相应的数据函数老出错......
C++
typedef enum {
RESULT_INITIALIZE = -1,
RESULT_SUCCESS = 0,
} Result_t;Result_t oemWaitForDecode(DWORD dwTime,
PTCHAR ptcMessage,
PTCHAR ptcCodeID,
PTCHAR ptcAimId,
PTCHAR ptcSymModifier,
PWORD pnLength, BOOL(*fpCallBack) (void));
c#如何调用这个函数接口?数据类型怎样对应?求详细指导?
请大牛们写下定义到使用代码提供我参考下吧。
C++
typedef enum {
RESULT_INITIALIZE = -1,
RESULT_SUCCESS = 0,
} Result_t;Result_t oemWaitForDecode(DWORD dwTime,
PTCHAR ptcMessage,
PTCHAR ptcCodeID,
PTCHAR ptcAimId,
PTCHAR ptcSymModifier,
PWORD pnLength, BOOL(*fpCallBack) (void));
c#如何调用这个函数接口?数据类型怎样对应?求详细指导?
请大牛们写下定义到使用代码提供我参考下吧。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;namespace ConsoleApplication6
{
class Program
{
[DllImport("DLL的名字", CharSet = CharSet.Ansi)]
public static extern Result oemWaitForDecode(uint dwTime, string ptcMessage, string ptcCodeID,
string ptcAimId, string ptcSymModifier, ref ushort pnLength, FPCallBack fp);
public delegate bool FPCallBack();
static FPCallBack fp;
static void Main(string[] args)
{
fp = fpCallBack;
//调用oemWaitForDecode
Console.ReadLine();
}
static bool fpCallBack()
{
//...
return true;
}
public enum Result
{
RESULT_INITIALIZE = -1,
RESULT_SUCCESS = 0
}
}
}试试看
多谢lizhibin11兄 我先试试o(∩_∩)o
public static extern Result oemWaitForDecode(uint dwTime, System.Text.StringBuilder ptcMessage, System.Text.StringBuilder ptcCodeID,
System.Text.StringBuilder ptcAimId, System.Text.StringBuilder ptcSymModifier, ref ushort pnLength, FPCallBack fp);
public delegate bool FPCallBack();
static FPCallBack fp;
static void Main(string[] args)
{ fp = fpCallBack;
System.Text.StringBuilder ptcMessage=new System.Text.StringBuilder(1024)
System.Text.StringBuilder ptcCodeID=new System.Text.StringBuilder(1024)
System.Text.StringBuilder ptcAimId=new System.Text.StringBuilder(1024)
System.Text.StringBuilder ptcSymModifier=new System.Text.StringBuilder(1024)
//
//调用oemWaitForDecode
}
兄:现在还是报出这样的异常我是在wince环境下调用的能帮我看看这个什么问题吗?
帮助文档上说了:
当使用 .NET Compact Framework 并对本机函数使用 P/Invoke 时,如果出现以下情况,则会引发此异常:托管代码中的声明不正确。.NET Compact Framework 不支持尝试执行的操作。DLL 名称在导出时难以分辨。在这样的情况下,请检查:任何违反 .NET Compact Framework P/Invoke 限制的行为。任何需要预分配内存的参数。如果存在这种情况,应传递对现有变量的引用。导出的函数的名称正确。这可以通过 DumpBin.exe 进行验证。您没有尝试传递过多的参数。未处理 System.NotSupportedException
Message="0x80131515"
StackTrace:
位于 SmartDeviceProject1.Form1.button1_Click(Object sender, EventArgs e)
位于 System.Windows.Forms.Control.OnClick(EventArgs e)
位于 System.Windows.Forms.Button.OnClick(EventArgs e)
位于 System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
位于 System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
位于 Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
位于 System.Windows.Forms.Application.Run(Form fm)
位于 SmartDeviceProject1.Program.Main()
COR_E_NOTSUPPORTED 0x80131515 -2146233067
首先要确认你的DLL是能支持wince环境的?
另外你的异常是停在oemWaitForDecode这句上?另外把代码贴出来
是停留在oemwaitfordecode这个函数上的异常。
这个是引用dll的C#相关代码:namespace oem5x00sdk
{
[StructLayout(LayoutKind.Sequential)]
public class oemdecodeceDll
{
public delegate bool fpCallBack();
[DllImport("oemdecodece.dll", EntryPoint = "oemWaitForDecode",CharSet=CharSet.Ansi)]
extern public static Result_t oemWaitForDecode(uint dwTime, System.Text.StringBuilder ptcMessage, System.Text.StringBuilder ptcCodeID,
System.Text.StringBuilder ptcAimId, System.Text.StringBuilder ptcSymModifier, ref ushort pnLength,
fpCallBack fp_CallBack);
}
public enum Result_t
{
RESULT_INITIALIZE = -1,
RESULT_SUCCESS = 0,
}
}
C#实现这个函数功能:
public bool p_callBack()
{
return true;
}
private void button1_Click(object sender, EventArgs e)
{
//const int MAX_MESSAGE_LENGTH=4096;
//string pMessage = MAX_MESSAGE_LENGTH.ToString();
//pMessage[0] = oem_waitfordecode.message;
//string pID = null;
//string pamID = null;
//string psmFier = null;
ushort plength=0;
System.Text.StringBuilder pMessage = new System.Text.StringBuilder(1024);
System.Text.StringBuilder pID = new System.Text.StringBuilder(1024);
System.Text.StringBuilder pamID = new System.Text.StringBuilder(1024);
System.Text.StringBuilder psmFier = new System.Text.StringBuilder(1024);
oem5x00sdk.oemdecodeceDll.fpCallBack fp_CallBack = new oem5x00sdk.oemdecodeceDll.fpCallBack(p_callBack);
int i=(int)oem5x00sdk.oemdecodeceDll.oemWaitForDecode(5000, pMessage, pID, pamID, psmFier,ref plength,fp_CallBack);
if ( i==(int)oem5x00sdk.Result_t.RESULT_SUCCESS)
MessageBox.Show("ok");
textBox1.Text =pMessage.ToString();
}C++引用这个函数功能的是这样的,我想用C#替换实现该怎么样实现?MAX_MESSAGE_LENGTH是接口里面定义的常量#define MAX_MESSAGE_LENGTH 4096;TCHAR bMessage[MAX_MESSAGE_LENGTH];
TCHAR code[1];TCHAR aim[1];TCHAR sym[1];
WORD nLength;
Result_t nResult = ::oemWaitForDecode(5000,&bMessage[0],&code[0],&aim[0],&sym[0],&nLength,&CB);
if(nResult == RESULT_SUCCESS)
{
::PlaySound(_T("\\Windows\\VoicBeep.wav"),NULL,SND_FILENAME|SND_ASYNC);
}
else
{
bMessage[0]=0x00;
}
SetDlgItemTextW(IDC_EDIT1,(LPCTSTR)bMessage);
补充下我还用c#调用了下dll里面的一个不带参数的函数是可以调用的.
{
public delegate bool fpCallBack();
[DllImport("oemdecodece.dll", EntryPoint = "oemWaitForDecode",CharSet=CharSet.Ansi)]
extern public static Result_t oemWaitForDecode(uint dwTime, ref char ptcMessage, ref char ptcCodeID,ptcAimId, ref char ptcSymModifier, ref ushort pnLength,fpCallBack fp_CallBack); oem5x00sdk.oemdecodeceDll.fpCallBack fp_CallBack = new oem5x00sdk.oemdecodeceDll.fpCallBack(p_callBack);
private void button1_Click(object sender, EventArgs e)
{
//const int MAX_MESSAGE_LENGTH=4096;
//string pMessage = MAX_MESSAGE_LENGTH.ToString();
//pMessage[0] = oem_waitfordecode.message;
//string pID = null;
//string pamID = null;
//string psmFier = null;
ushort plength=0;
char[] pMessage = new char[4096];
char[] pID = new char[2];
char[] pamID = new char[2];
char[] psmFier = new char[2]; int i=(int)oem5x00sdk.oemdecodeceDll.oemWaitForDecode(5000, ref pMessage[0],ref pID[0], ref pamID[0],ref psmFier[0],ref plength,fp_CallBack);
if ( i==(int)oem5x00sdk.Result_t.RESULT_SUCCESS)
MessageBox.Show("ok");
textBox1.Text =pMessage.ToString();
}C++中CB是如何赋值的
BOOL CB(void)
{
return TRUE;
}
未处理 System.NotSupportedException
Message="0x80131515"
StackTrace:
位于 SmartDeviceProject1.Form1.button1_Click(Object sender, EventArgs e)
位于 System.Windows.Forms.Control.OnClick(EventArgs e)
位于 System.Windows.Forms.Button.OnClick(EventArgs e)
位于 System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
位于 System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
位于 Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
位于 System.Windows.Forms.Application.Run(Form fm)
位于 SmartDeviceProject1.Program.Main()
大牛们快来啊!搞的我快要吐血了!
搞定了我开帖给散分.
[DllImport("oemdecodece.dll", EntryPoint = "oemWaitForDecode",CharSet=CharSet.Ansi)]
extern public static Result_t oemWaitForDecode(uint dwTime, ref char ptcMessage, ref char ptcCodeID,ptcAimId, ref char ptcSymModifier, ref ushort pnLength,ref fpCallBack
最后一个参数再改下试试
也有可能和下面的问题一样
http://topic.csdn.net/u/20110216/10/20386e55-f99d-499a-9041-06e48b2228a1.html
还是不行 我都试的快吐了......
using System.Text;
using System.Runtime.InteropServices;namespace ConsoleApplication11
{
class Program
{
[DllImport("DLL的名字", CharSet = CharSet.Ansi)]
public static extern Result oemWaitForDecode(uint dwTime, IntPtr ptcMessage,
IntPtr ptcCodeID, IntPtr ptcAimId, IntPtr ptcSymModifier,
IntPtr pnLength, FPCallBack fp);
public delegate bool FPCallBack();
static FPCallBack fp;
static byte[] ptcmessage = new byte[4096];
static byte[] ptccodeid = new byte[1];
static byte[] ptcaimid = new byte[1];
static byte[] ptcsymmodifier = new byte[1];
static ushort[] pnlength = new ushort[1];
static void Main(string[] args)
{
fp = fpCallBack;
IntPtr message = Marshal.UnsafeAddrOfPinnedArrayElement(ptcmessage, 0);
IntPtr codeid = Marshal.UnsafeAddrOfPinnedArrayElement(ptccodeid, 0);
IntPtr aimid = Marshal.UnsafeAddrOfPinnedArrayElement(ptcaimid, 0);
IntPtr symmodifier = Marshal.UnsafeAddrOfPinnedArrayElement(ptcsymmodifier, 0);
IntPtr length = Marshal.UnsafeAddrOfPinnedArrayElement(pnlength, 0);
Result result = oemWaitForDecode(5000, message, codeid, aimid,
symmodifier, length, fp);
Console.ReadLine();
}
static bool fpCallBack()
{
//...
return true;
}
public enum Result
{
RESULT_INITIALIZE = -1,
RESULT_SUCCESS = 0
}
}
}
using System.Text;
using System.Runtime.InteropServices;namespace ConsoleApplication11
{
class Program
{
[DllImport("DLL的名字", CharSet = CharSet.Ansi)]
public static extern Result oemWaitForDecode(uint dwTime, IntPtr ptcMessage,
IntPtr ptcCodeID,
IntPtr ptcAimId,
IntPtr ptcSymModifier,
IntPtr pnLength,
FPCallBack fp);
public delegate bool FPCallBack();
static FPCallBack fp;
static char[] ptcmessage = new char[4096];
static char[] ptccodeid = new char[1];
static char[] ptcaimid = new char[1];
static char[] ptcsymmodifier = new char[1];
static ushort[] pnlength = new ushort[1];
static void Main(string[] args)
{
fp = fpCallBack;
IntPtr message = Marshal.UnsafeAddrOfPinnedArrayElement(ptcmessage, 0);
IntPtr codeid = Marshal.UnsafeAddrOfPinnedArrayElement(ptccodeid, 0);
IntPtr aimid = Marshal.UnsafeAddrOfPinnedArrayElement(ptcaimid, 0);
IntPtr symmodifier = Marshal.UnsafeAddrOfPinnedArrayElement(ptcsymmodifier, 0);
IntPtr length = Marshal.UnsafeAddrOfPinnedArrayElement(pnlength, 0);
Result result = oemWaitForDecode(5000, message, codeid, aimid, symmodifier, length, fp);
Console.ReadLine();
}
static bool fpCallBack()
{
//...
return true;
}
public enum Result
{
RESULT_INITIALIZE = -1,
RESULT_SUCCESS = 0
}
}
}
好像在。netcf库中没有该方法......
您的意思重新封装这个旧的dll导出函数oemWaitForDecode实现到新的dll供c#引用,可以不用对应参数引用?
是这样的话C++重新写dll封装导出函数实现没做过不知道怎么做?
还请麻烦详细指教啊......先谢xingyuebuyu兄了!
你还不休息啊o(∩_∩)o 小心lp不让你上床