C#调用C++DLL结构体指针传参问题,求大神现身指导,在线等!!!

c++方法:

 extern "C" _declspec(dllexport) bool GetKE(string CaseId, string CaseText, string &CaseReason, SS *KeList,int &nLen_KeList);

c++结构体:

 typedef struct BriefKE
{
    string KeName;
    string KeValue;
}SS;

C#结构体:

 [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]
    public struct BriefKE 
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string KeName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string KeValue;

    }

C#函数:

 [DllImport("KnowledgeExtract.dll", EntryPoint = "GetKE")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public unsafe static extern bool GetKE(StringBuilder CaseId, StringBuilder CaseText, ref StringBuilder CaseReason, BriefKE[] infos, ref int nLen_KeList);

C#调用:

//结构体数组指针初始化
SS[] KeList = new SS[20];
                for (int i = 0; i < KeList.Length; i++)
                {
                    KeList[i] = new SS();
                }
                IntPtr[] ptArray = new IntPtr[1];
                ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SS)) * 20);
                IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SS)));
                Marshal.Copy(ptArray, 0, pt, 1);
//其他几个参数初始化省略
 if (!test.GetKE(caseId, caseText, ref caseReason, infos, ref len))
                {
                    Console.WriteLine("读取失败");
                    Console.ReadLine();
                }

总是报下面这个错,请大神给写一个C#调用的例子

 “System.AccessViolationException”类型的未经处理的异常在 未知模块。 中发生 

其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

1个回答

C++的string和C#的string不兼容,C++那里最好用字符数组。

zxb0202
法号晋空 C++DLL是别人提供的,没法改。C++里的string和C#里的StringBuilder是对应的吧?应该不是这块的问题,应该是结构体指针的问题,但不知道怎么解决
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
c#调用c写的dll出错,求大神指导!!!
rn[code=csharp]rn [DllImport("__BANKER.dll", EntryPoint = "getdata", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]rn public static extern double getdata(StringBuilder sb, int nLen);rn[/code]rnrnrn这是c写的dll代码声明rn[code=c]rnextern "C" __declspec(dllexport) double zhuang(char *array,int N);rn[/code]rnrn结果:rn“尝试读取或写入受保护的内存,这通常表明其他内存已损坏。。。。”rn网上搜索很多是类型的问题,之前C#用的是char[],出问题,现在改成string ,stringbuilder还是这样。。。。
求大神指导,在线等
[img=https://img-bbs.csdn.net/upload/201306/06/1370494280_301437.jpg][/img]
C#调用带结构体指针的C Dll的方法
在C#中调用C(C++)类的DLL的时候,有时候C的接口函数包含很多参数,而且有的时候这些参数有可能是个结构体,而且有可能是结构体指针,那么在C#到底该如何安全的调用这样的DLL接口函数呢?本文将详细介绍如何调用各种参数的方法。 一、调用接口仅含普通变量 int fnAdd(int num1,int num2); 那么在C#调用这种函数最简单了,直接用函数原型即可,如下: [Dll...
C#调用C封装dll结构体指针
原来一直用MFC做,最近项目要用C#学了学基本的东西还是有差距啊,调用底层函数的时候卡主了,求路过大神帮忙rn先是C++的定义:rntypedef struct TagData_structrn double value; //返回点的数值rn long time; //返回点的时间,秒rn int status; //返回点的状态rnTagData;rnrn初始化:rnchar *tagNames[MEM_PAGE_ITEMS];rnfor ( int i=0; i
c# 调用dll 传参问题
各位大大,求赐教啊。rn小弟要调用一个现成的DLL,原来配套的调用例子是VB写的,声明如下rnPublic Declare Function WrKbByteA Lib "KEYPORT.DLL" (ByVal val As Byte) As Long,rn调用如下rnCall WrKbByteA(&HC4),rn小弟要改成C#版本的,我的方式是:rn声明:rn[DllImport("KEYPORT.dll", CallingConvention = CallingConvention.Cdecl)]rnpublic static extern int WrKbByteA(Byte Key);rn调用方法:rnByte key = 0xC4;rnWrKbByteA(key);rn运行没有反映,求指教啊,谢谢!!!!
c# 调用dll中结构体指针的问题
vc函数定义如下 rntypedef struct _GroupCard rn rn DWORD cardNo; rn WORD reserved; rn WORD wCardCount; rn _GroupCard * pNext; //pointer to the next group card. rn TGroupCard, *PGroupCard; rnrnint GetCardReg(BYTE ComPort, BYTE CtrlID, WORD Offset, WORD GroupCount, WORD *GroupCountGet, TGroupCard *lpGroupCard); rnrn我想在c#中调用,应该怎么写接口函数?
求c#大神指导
我想指导为什么创建不了用户 (NGUSER)?????rnrnrnrnrn private void btnSetNg_Click(object sender, EventArgs e)rn rn string strSql = "create user NGUSER identified by '9003' default tablespace std; grant connect,resource to NGUSER ; grant create any sequence to NGUSER ; grant create any table to NGUSER ; grant delete any table to NGUSER ; grant insert any table to NGUSER ; grant select any table to NGUSER ; grant unlimited tablespace to NGUSER ; grant execute any procedure to NGUSER ; grant update any table to NGUSER ; grant create any view to NGUSER ; "; rn rn //"CREATE USER NGUSER PROFILE DEFAULT IDENTIFIED BY 9003 DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP ACCOUNT UNLOCK;GRANT CREATE ANY TABLE TO NGUSER;GRANT UNLIMITED TABLESPACE TO NGUSER;GRANT CONNECT TO NGUSER;GRANT RESOURCE TO NGUSER;";rn rn tryrn rn Form1.oclConnection.Open();rn rn OracleCommand oclCommand = new OracleCommand(strSql, Form1.oclConnection);rn rnrn MessageBox.Show("创建成功");rnrn /*rn rn rn create user NGUSER identified by 9003; grant create session to NGUSER; grant create table to NGUSER; grant create tablespace to NGUSER; grant create view to NGUSER; rn rn */rnrnrn rn catch (Exception ex)rn rnrn MessageBox.Show("SQL语句出错!\n" + strSql.ToString() + "\n请检查:\n" + ex.ToString());rnrn rn finallyrn rn Form1.oclConnection.Close();rn rn FormEnterDatabase_Load(this,e);rn rn
C#求大神指导。。。
有一张表。。rn rntable arn(rnno char(9),rnna char(8),rnse char(4)rnrn)rnrn在连接数据库SQL server08后,我想将数据都显示在窗体中。。怎么实现呢。。求指点。。。
c# 调用DLL问题 在线等!!!
我现在有个开发商提供的动态链接库,SMSDLL.dll,收发短信用的。rn里面的函数包括:rn#pragma oncerntypedef struct _sms_report_t_rnrn DWORD index; //短消息编号:index,从0开始递增rn char Msg[256]; //短信内容rn int Success; //是否发送成功 0为失败,非0为成功rn char PhoneNo[32]; //目标手机号码rn SMSReportStruct;rnrntypedef struct _sms_msg_t_rnrn char Msg[256]; //短信内容rn char PhoneNo[32]; //对方手机号码rn char ReceTime[32]; //接收时间rn SMSMessageStruct;rnrnrn //启动服务,打开串口,初始化Modem, 0为失败,非0为成功rn //校验位, EvenParity :0,MarkParity:1,NoParity:2,OddParity:3,SpaceParity,4rn //停止位 OneStopBit 0,OnePointFiveStopBits:1,TwoStopBits 2rn //流控:NoFlowControl:0, CtsRtsFlowControl:1, CtsDtrFlowControl:2, DsrRtsFlowControl:3, DsrDtrFlowControl:4, XonXoffFlowControl:5rn int _stdcall SMSStartService(int nPort,DWORD BaudRate = 57600, int Parity=2, int DataBits = 8,int StopBits=0,int FlowControl=0,char* csca="card");rnrnrn //停止服务,并关闭串口,0为失败,非0为成功rn int _stdcall SMSStopSerice();rnrn //发送短消息,返回短消息编号:index,从0开始递增,该函数不会阻塞,立既返回,请用函数SMSQuery(DWORD index)来查询是否发送成功rn DWORD _stdcall SMSSendMessage(char* Msg,char* PhoneNo);rnrnrn //报告短信发送壮态(成功与否)0为有报告,非0为无rn int _stdcall SMSReport(SMSReportStruct* rept); rnrnrnrn //查询指定序号的短信是否发送成功(该序号由SMSSendMessage返回)rn //返回 0 表示发送失败rn // 1 表示发送成功rn // -1 表示没有查询到该序号的短信,可能仍在发送中。rn int _stdcall SMSQuery(DWORD index);rnrnrn //接收短信,0为有短信,非0为无rn int _stdcall SMSGetNextMessage(SMSMessageStruct* Msg);rnrnrn //返回错误内容的长度rn int _stdcall SMSGetLastError(char* err);rnrnrn这些函数和结构体 我怎么才能在c# winform中使用啊?
链表模板类传参遇到问题,求大神指导
[code=c][/code]最近在研究模板类,准备用其实现一个类似C#中List的容器rn,以List< List<......> >实现多维数组的功能rn我写了个list模板类,是基于结构体node的链表rn[code=c]template< class anytype >rnstruct nodernrn anytype value;rn node* next; //指向下个结点rn;rn[/code]rn类的成员列表如下rn[code=c]template< class anytype >rnclass listrnrnpublic:rn list()rn rn count = 0;rn head.next = NULL;rn rnrn ~list()rn rn clear(); rn rnrn int get_count()rn rn return count;rn rn void add();rn void add( const anytype &c );rn bool dlt( const int &i );rn void go_through(); //遍历rn bool insert( const int &i, const anytype &c );rn void clear();rn anytype & operator [] ( const int &i );rn int seek( const anytype &model ); //返回值-1表示没找到rn //友元列表rn rnprivate:rn node head;rn int count;rn;[/code]rn不带参数的add()函数如下rn[code=c]template< class anytype >rnvoid list::add()rnrn node* p = &head;rnrn while ( p -> next != NULL )rn p = p -> next;rnrn p -> next = new node;rn// p -> next -> value = c;rn p -> next -> next = NULL;rnrn count++;rn[/code]rn带参数的add( anytype )如下[code=c]template< class anytype >rnvoid list::add( const anytype &c )rnrn node* p = &head;rnrn while ( p -> next != NULL )rn p = p -> next;rnrn p -> next = new node;rn p -> next -> value = c;rn p -> next -> next = NULL;rnrn count++;rn[/code]rn对于一维数组来说这个类并没什么问题,rn多维数组使用无餐的add()来申请内存,带参数的add( anytype )rn来给一维的链表添加带初值的结点也没问题。使用方法如下rn[code=c]list a;rn list< list > b;rn list< list< list > > d;rnrn a.add( 10 );rn a.add( 50 );rn a.add( 79 );rn a.go_through();rnrn b.add();rn b[1].add( 33 );rn b[1].add( 3 );rn b[1].add( 78 );rn b[1].go_through();rn b.add();rn b[2].add( 888 );rn b[2].go_through();[/code]rn我想写这样重载add()函数,
求大神指导求大神指导求大神指导
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall CSpeechRecognition1::CSpeechRecognition1(void)" (??0CSpeechRecognition1@@QAE@XZ),该符号在函数 "public: __thiscall main::main(class CWnd *)" (??0main@@QAE@PAVCWnd@@@Z) 中被引用rn1>main.obj : error LNK2001: 无法解析的外部符号 "public: virtual int __thiscall main::OnInitDiaLog(void)" (?OnInitDiaLog@main@@UAEHXZ)rn1>main.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall CSpeechRecognition1::~CSpeechRecognition1(void)" (??1CSpeechRecognition1@@UAE@XZ),该符号在函数 "public: virtual __thiscall main::~main(void)" (??1main@@UAE@XZ) 中被引用rn1>D:\tttttt\RecordSpeech\Debug\RecordSpeech.exe : fatal error LNK1120: 3 个无法解析的外部命令
求MFC大牛现身指导~~
用MFC已经有好几个月了,今天碰到了个棘手的问题。自己捣鼓了半天没弄出来所以就想贴上来求助了。rn因为程序代码比较长,其中大量的是计算,不太好往上贴,贴了估计也看不清。rn大致情况是:rnrn首先,我的程序有一个输入面板(参数很多)。然后输入完成,用户选择开始计算。rn因为计算耗时比较长,所以我的想法是做一个过渡动画,在计算过程中给用户显示,以免让用户感觉是死机了。rnrn1.当用户点击“开始计算”,我就弹出一个弹窗(上面包含有一个进度条,自己画的)。rn2.紧接着立即开启一个线程(我用API函数CreateThread直接开的),在线程中计算。rn3.在计算过程中,我设置了三个节点,运算到相应的语句时,由线程给上面的弹窗发一个消息,提示它更新进度条。rn4.计算完毕,立即关闭弹窗,然后,判定结果是否合理,如果合理,立即再做一个弹窗显示结果。rnrn说明:所有弹窗都是非模态的。rnrn问题:rn每次关闭程序的时候,都会报错,感觉像是内存泄漏。我实验过,如果把那个过渡弹窗注释掉,就不会报错了,同样,如果把结果显示的弹窗注释掉,同样不会报错。rn我怀疑是不是两个弹窗的问题。rn下面是报错截图,debug下的,应该也看不出来啥信息吧。rn有时候是整个程序退出的时候才报错。rn[img=https://img-bbs.csdn.net/upload/201406/26/1403793439_828469.jpg][/img]rn
求大神指点!C# post传参问题
本人新手,用C#开发。别人给我一个接口地址http://XXXXXXXXXXXXX/ ,有四个参数userid(帐号) pwd(密码)vehiclenum (车号) itype (用户类型 用C),说是采用post方法传递,返回数据用需用 Unescape进行解码,数据是用“|”分隔的字符串,如果第一个是0 那么后面的数据如下[车号],[发动机号],[颜色],[车型].......我现在想取[color=#FF0000]车号[/color]的值,应该怎么做?求代码!!我学习下。rn
关于C#调用C++ dll传参的问题
C++ DLL中rnint Parse(char* Path,char *PassWord)rnrn rnC#中调用 rn[DllImport("abc.dll")]rnstatic extern int Parse(string path, string password);rnrn在C#里调用Parse,password这个参数传过来总是不对,解密的时候会报错。但是直接在C++dll的方法里给rnPassWord赋值PassWord=“3232323”;这样就没有问题。可是为什么传进来的参数就不对呢?rnrn我用MessageBox显示这个password显示也没有问题啊。莫非是因为C#的string结尾没有'/0'?rn但是我用C++理由sizeof(PassWord)得到的值总是4,那位高人遇到过这种问题啊?给点思路
DLL调用问题,在线等
我用VB一个VC6.0写的dll,调用其中的函数时,返回正确执行的信息,却不是所要的结果,比如:想的结果为:12235,可是返回来的却是一些很奇怪的东东
DLL 调用问题 在线等
我用delphi 写的DLL 在DELPHI中使用时申明是rn Function MyMax ( X , Y : integer ) : integer ; stdcall ; external 'Project1.dll' ;rn那我在vc中要用这个函数 应该怎么做了 rnrn谢谢
C# 如何调用DLL函数返回的结构体指针
/*下面是C代码,最终编译成DLL库,库名为student.dll*/rnstruct studentrnrn char name[123];rn int age;rn;rnrnstruct student* GetStudent()rnrn struct student *p_my_student = (struct student*)malloc(sizeof(struct student)*sizeof(char));rn strcpy( p_my_student.name,"David");rn p_my_student.age = 28;rn return (p_my_student);rnrnrnrn/*下面为C#代码,使用非安全方式调用dll库*/rnnamespace studentIntfrnrn public partial class Form1 : Formrn rn public Form1()rn rn InitializeComponent();rn rn [StructLayout(LayoutKind.Sequential)] rn public struct studentrn rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] rn public byte[] name;rn int age;rn ;rn [DllImport(UTSQLITE_DLL)]rn public unsafe static extern student* GetStudent();rn private void button1_Click(object sender, EventArgs e)rn rn unsafern rn GetStudent();rn rn rn rnrnrn上述代码编译通不过,错误提示是:无法获取托管类型(“studentIntf.student”)的地址和大小,或无法声明指向它的指针rn 请问:即然使用了非安全(unsafe)方式,还不能返回结构体的指针吗?rnrn接下来我用下面的方式(IntPtr)来调用库rn namespace studentIntfrnrn public partial class Form1 : Formrn rn public Form1()rn rn InitializeComponent();rn rnrn [StructLayout(LayoutKind.Sequential)] rn public struct studentrn rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] rn public byte[] name;rn int age;rn ;rn [DllImport(UTSQLITE_DLL)]rn public unsafe static extern IntPtr GetStudent(); /*区别*/rn private void button1_Click(object sender, EventArgs e)rn rn unsafern rn IntPtr = GetStudent(); /*区别*/rn rnrn rn rnrnrn 上述代码编译通过,但我不知道如何将IntPtr转换成struct student*,请问如何转换?或者有什么更好的办法来处理DLL返回结构体指针的问题rn
Dll 调用问题(在线等)
在函数使用过程出现错误如下:(弹出的异常错误)rnrnFile:i386\chkesp.crnrnThe value Esp was not properly saved across a function call.This isrnusually a result of clling a function declared with one calling convention with a function pointer declared with a different calling convention.rnrn调用过程:rnrntypedef double (* _SENDMESSAGE)(LPTSTR AppID, LPTSTR Password, LPTSTR MobilePhone, LPTSTR Content,rn LPTSTR Reply,rn double Ack, LPTSTR ID, double Rep,rn double Priority,rn double MessageType);rnrn HINSTANCE hLibrary;rn_SENDMESSAGE sendMessage;rnrnhLibrary=LoadLibrary("SCS.dll");rn rn sendMessage=(_SENDMESSAGE)GetProcAddress(hLibrary,"sendMessage");rnrn//使用rnif( sendMessage(app,pwd,dst,cont,rep,1,ID,2,9,0)==0) //在这里出错rn
dll调用问题,在线等
现在在研究IKEY的PKI开发rnrn它提供了c库和一个DLL,但是我要用VB做,只能调用DLL中的函数,rn比如 它提供的c例子是这样写的rnrv = C_Initialize(NULL_PTR);rnrv是一个长整型(unsigned long int )的返回值,rn参数是空指针吧,它是这样定义的:#define NULL_PTR 0rnrn我用工具看了一下,dll中确实有这个函数 C_Initializernrn我在vb中rnPublic Declare Function C_Initialize Lib "C:\WINNT\system32\K1PK112.DLL" (ByRef lngArg As Long) As Longrnrn然后rn Dim rv As Longrn Dim lngtemp As Longrn rv = C_Initialize(lngtemp)rnrn编译的时候报错: DLL调用约定错误rnrn这里到底该怎么改呢?请教大家
C# 如何传递结构体指针到C DLL 里面
C DLL 代码如下:rnstruct tagTest2rnrnint aa;rnint bb;rn;rnrnstruct tagTestrnrnBYTE a;rnchar ch[10];rnint bb;rnvoid *pData; //用于传递tagTest2rn;rnrnextern "C" __declspec(dllexport) HANDLE test3(tagTest *pTest)rnrnpTest->a = 100;rnpTest->bb = 200;rnrntagTest2 *pData = (tagTest2*)pTest->pData;rnrnpData->aa = 300;rnpData->bb = 600;rnrnrnreturn pTest;rnrnrn在C# 中的定义:rnnamespace testmydllrnrn[StructLayout(LayoutKind.Sequential)] rnstruct tagTestrnrn[MarshalAs(UnmanagedType.U1)]rnpublic byte a;rn[MarshalAs(UnmanagedType.ByValArray,SizeConst=10)]rnpublic char[] ch;rn[MarshalAs(UnmanagedType.U4)]rnpublic int bb;rn[MarshalAs(UnmanagedType.U4)]rnpublic IntPtr pData;rnrn;rnrn[StructLayout(LayoutKind.Sequential)] rnstruct tagTest2rnrn[MarshalAs(UnmanagedType.U4)]rnint aa;rn[MarshalAs(UnmanagedType.U4)]rnint bb;rn;rnrnrnclass testdllrnrn[DllImport("testdll.dll")] rnpublic static extern IntPtr test3(ref tagTest pTest);rnrnrnrnrnrn请问在C# 中这样定义是否正确? 我定义了一个tagTest对象 ,怎么传递tagTest2 对象的指针到tagTest中的pData变量? 谢谢了.
关于C#调用C的DLL传参问题(急急)!
C语言中封装的方法:rnint winsjjh( const char * forms, const char *dwbh, const char *instr, rn    const char * infile, char *outstr, char *outfile )rnforms:编码rndwbh:编rninstr:上传结构rninfile:上传文件名rnoutstr:下传结构rnoutfile:下传文件名rnrn上传下传结构定义:rnstruct djz rnrn  char jym[7]; rn  char dwbh[9]; rn  char wdlsh[10]; rn  char czy[5]; rn char jyrq[9]; rn char dkpz[10]; rn  char cch[3];  rn  char ccms[40];  rn char rev1[30]; rn char rev2[100]; rnrnrnrnusing System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Text;rnusing System.Windows.Forms;rnusing System.Runtime.InteropServices;rnrnnamespace BankConnectFormrnrnrn [StructLayout(LayoutKind.Sequential)]rn public struct djzrn rn public string jym;rn public string dwbh; rn public string wdlsh; rn public string czy; rn public string jyrq; rn public string dkpz; rn public string cch ;rn public string ccms;rn public string rev1; rn public string rev2;rn rnrn public partial class FrmBank : Formrn rn [DllImport("netwin.dll")]rn public static extern int winsjjh(rn string forms,rn string dwbh,rn djz instr,rn string infile,rn ref djz outstr,rn string outfilern );rn public FrmBank()rn rn InitializeComponent();rn rn private void btnUP_Click(object sender, EventArgs e)rn rn djz d1=new djz();rn d1.jym = "jym";rn d1.dwbh = "dwbm";rn d1.wdlsh = "wdbh";rn d1.czy = this.textBox4.Text.ToString().Trim();rn d1.jyrq = this.dateTimePicker1.Value.ToString("yyyyMMdd");rn string infile=this.txtFilePath.Text .ToString ();rn djz d2=new djz();rn int i;rn string strTemp="";rn i=winsjjh("116102", "38161720",d1, infile, ref d2, strTemp); rn rnrn rnrnrn返回的i一直不对,请高手指点下rn,这里的参数该如何匹配??rnrnrn
c#调用c++dll 结构体指针的问题
c++dl函数rn int _stdcall TA_ReadCardSimple(AccountMsg * pAccMsg);rn结构体rntypedef structrnrn char Name[21]; /*姓名*/rn char SexNo[2]; /*性别*/rn char DeptCode[19]; /*部门代码*/rn unsigned int CardNo; /*卡号*/rn unsigned int AccountNo; /*帐号*/rn char StudentCode[21]; /*学号*/rn char IDCard[21]; /*身份证号*/rn char PID[3]; /*身份代码*/rn char IDNo[13]; /*身份序号*/rn int Balance; /*现余额*/rn char Password[7]; /*消费密码*/rn char ExpireDate[7]; /*账户截止日期*/rn unsigned short SubSeq; /*补助戳*/rn char IsOpenInSys; /*是否在本系统内开通*/rn short TerminalNo; /*终端号码,提取补助时需要填写*/rn short RetCode; /*后台处理返回值*/rn char Flag[16]; /*状态(2004-08-26增加)*/rn char Pad[84]; /*预留字段*/rn AccountMsg;rnrn我在c#里面引用rn[DllImport("AIO_API.dll")]rn public static extern int TA_ReadCardSimple(ref IntPtr pAccMsg);rn定义结构体rn[StructLayout(LayoutKind.Sequential)]rn public struct AccountMsgrn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]rn public byte[] Name; /*姓名*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]rn public byte[] SexNo; /*性别*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19)]rn public byte[] DeptCode; /*部门代码*/rn [MarshalAs(UnmanagedType.U4)]rn public uint CardNo; /*卡号*/rn [MarshalAs(UnmanagedType.U4)]rn public uint AccountNo; /*帐号*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]rn public byte[] StudentCode; /*学号*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 21)]rn public byte[] IDCard; /*身份证号*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]rn public byte[] PID; /*身份代码*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 13)]rn public byte[] IDNo; /*身份序号*/rn [MarshalAs(UnmanagedType.U4)]rn public uint Balance; /*现余额*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]rn public byte[] Password; /*消费密码*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 7)]rn public byte[] ExpireDate; /*账户截止日期*/rn [MarshalAs(UnmanagedType.U4)]rn public uint SubSeq; /*补助戳*/rn [MarshalAs(UnmanagedType.U1)]rn public byte IsOpenInSys; /*是否在本系统内开通*/rn [MarshalAs(UnmanagedType.U4)]rn public uint TerminalNo; /*终端号码,提取补助时需要填写*/rn [MarshalAs(UnmanagedType.U4)]rn public uint RetCode; /*后台处理返回值*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]rn public byte[] Flag; /*状态(2004-08-26增加)*/rn [MarshalAs(UnmanagedType.ByValArray, SizeConst = 84)]rn public byte[] Pad; /*预留字段*/rnrnrn调用 IntPtr[] ptArray = new IntPtr[1];rn ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AccountMsg)) * 1);rn IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AccountMsg)));rn Marshal.Copy(ptArray, 0, pt, 1); rn int a = TA_ReadCardSimple(ref pt); rn msg1[0] =rn (AccountMsg)Marshal.PtrToStructure((IntPtr)((UInt32)pt + 0 * Marshal.SizeOf(typeof(AccountMsg)))rn , typeof(AccountMsg));rn MessageBox.Show(Convert.ToByte(msg1[0].Name[0]).ToString());rn在倒数第二句报引发类型为“System.ExecutionEngineException”的异常。谁能帮看看谢谢rn
WPF 调用 dll 求指导
“MachineDll” 在WINFOM 里面可以直接引用,所以调用“ MachineDll.StatusCaptureErrorEventHandler” 这个事件和machine.Close()这个方法,代码如下rnrnusing MachineDll;rnrnnamespace Testrnrn public partial class Form1 : Formrn rn private StatusCapture machine;rn rn private void OnClosing(CancelEventArgs e)rn rn machine.Close();rn base.OnClosing(e);rn rn private void button4_Click(object sender, EventArgs e)rn rn if (machine == null)rn rn machine = new StatusCapture();rn machine.OnEventError += new MachineDll.StatusCaptureErrorEventHandler(machine_OnEventError);rn machine.OnEventStatus += new MachineDll.StatusCaptureEventHandler(machine_OnEventStatus);rn machine.Start((ECOM)Enum.Parse(typeof(ECOM), comboBox1.SelectedItem.ToString()), 10);rn rn rnrnrn使用wpf项目不能直接引用“MachineDll” ,所以使用了’[DllImport("Machine.dll")]‘ rnrn machine.Close()这个方法 可以 声明下面内容 以便于调用rn [DllImport("Machine.dll")]rn public extern void Close();rnrn那 这个 “ MachineDll.StatusCaptureErrorEventHandler” 应该咋么声明?rnrn
结构体指针链表,编译报错,求指导!!
[code=c]#includern#includern#includern#includern#includernstruct Family *get_person(void);//输入个人信息 rnbool related(struct Family *pmember1,struct Family *pmember2);//测试他爷爷、他奶奶 rnbool set_ancestry(struct Family *pmember1,struct Family *pmember2);rnstruct Daternrn int day;rn int month;rn int year;rn;rnstruct Familyrnrn struct Date dob;rn char name[20];rn char father[20];rn char mother[20];rn struct Family *next;rn struct Family *previous;rn struct Family *p_to_pa;rn struct Family *p_to_ma;rn;rnint main(void)rnrn struct Family *first=NULL;rn struct Family *current=NULL;rn struct Family *last=NULL;rn //struct Family *pmember1=NULL;rn //struct Family *pmember2=NULL;rn char more='\0';rn for(;;)rn rn printf("\nDo you want to details of a%s person (Y or N)?",first !=NULL?"nother":"");rn scanf(" %c",&more);rn if(tolower(more)=='n')rn break;rn current=get_person();rn if(first==NULL)rn rn first=current;rn last=current;rn rn elsern rn last->next=current;rn current->previous=last;rn last=current;rn rn current=first;rn while(current->next !=NULL)rn rn int parents=0;rn last=current->next;rn while(last !=NULL)rn rn if(related(current,last))rn if(++parents==2)rn break;rn last=last->next;rn rn current=current->next;rn rn rn /*current=first;rn while(current->next !=NULL)rn rn int parents=0;rn last=current->next;rn while(last !=NULL)rn rn if(related(current,last))rn if(++parents==2)rn break;rn last=last->next;rn rn current=current->next;rn */rn current=first;rn while(current !=NULL)//按顺序输出名字,日,月、年、父亲、母亲,及认爷爷,奶奶 rn rn printf("\n%s was born %d/%d/%d,and has %s and %s as parents.",current->name,current->dob.day,current->dob.month,current->dob.year,current->father,current->mother);rn if(current->p_to_pa !=NULL)rn printf("\n\t%s's birth date is %d/%d/%d",current->father,current->p_to_pa->dob.day,current->p_to_pa->dob.month,current->p_to_pa->dob.year);rn if(current->p_to_ma !=NULL)rn printf("\n\t%s's birth date is %d/%d/%d",current->mother,current->p_to_ma->dob.day,current->p_to_ma->dob.month,current->p_to_ma->dob.year);rn current=current->next;rn rn current=first;rn while(current !=NULL)//释放内存 rn rn last=current;rn current=current->next;rn free(last);rn rn system("pause");rn return 0;rnrnstruct Family *get_person(void)rnrn struct Family *temp;rn temp=(struct Family*)malloc(sizeof(struct Family));rn printf("\nEnter the name of the person:");rn scanf("%s",temp->name);rn printf("\nEnter %s's date of birth (day month year):",temp->name);rn scanf("%d %d %d",&temp->dob.day,&temp->dob.month,&temp->dob.year);rn printf("\nWho is %s's father?",temp->name);rn scanf("%s",temp->father);rn printf("\nWho is %s's mother?",temp->name);rn scanf("%s",temp->mother);rn temp->next=temp->previous=NULL;rn temp->p_to_pa=temp->p_to_ma=NULL;rn return temp;rnrnbool set_ancestry(struct Family *pmenber1,struct Family *pmenber2)//测试A是否是B的爷爷/奶奶 rnrn struct Family *pmember1;rn struct Family *pmember2;rn if(strcmp(pmenber1->father,pmenber2->name)==0)rn rn pmember1->p_to_pa=pmember2;rn return true;rn rn if(strcmp(pmember1->mother,pmenber2->name)==0)rn rn pmember1->p_to_ma=pmember2;rn return true;rn rn elsern return false;rnrnbool related(struct Family *pmember1,struct Family *pmember2)rnrn return set_ancestry(pmember1,pmember2)||set_ancestry(pmember2,pmember1);rn[/code]
c 语言的问题求大神的指导!
#include"stdio.h"n#include"stdlib.h"nvoid main()nn int guess=0,problem,ch;n ch=getchar();n don scanf("%d",&guess);n problem=(int)(rand()%100)+1;n for(;guess!=problem;)n if(guess>problem)n printf("too high!");n if(guess
c语言,打孔问题,求大神指导。
题目,s得到一个数,他想知道这个数每一位上的数字的孔数之和,其中,1,2,3,5,7这几个数字是没有孔的,0,4,6,9都只有一个孔,而8有两个孔。
c小白,求大神指教,在线等!!!
//循环录入性别,男只能用m/M表示,女只能用f/F表示rn//一旦录入错误,则结束录入,并输入录入正确次数rn#includern int main()rnrn char i;rn int n=0;rn while(1)rn rnrn printf("请输入性别:");rn scanf("%c",&i);rn n++;rn fflush(stdin);rn if(i!='m'||i!='M'||i!='F'||i!='f')rn rn printf("输入错误,结束输入!\n");rn break;rn rn rn printf("一共输入正确%d次",n);rn return 0;rnrn为什么得不到自己想要的结果
c语言问题求大神指导!!
#include"stdio.h"n#include"stdlib.h"nvoid main()nn int problem,guess=1,a;n char ch;n problem=(int)(rand()%899+1);n scanf("%f%d",&ch,&guess);n for(a=0;ch!='n';a++)n for(a=0;guess==problem;a++)n if(guess>problem)n printf("too high");n if(guess
c语言的问题求大神的指导!!!
#include"stdio.h"nmain()nn int a[10],i,j,k;n for(i=0;i<10;i++)n scanf("%d",&a[i]);n for(i=0;i<10;i++)n printf("%5d",a[i]);n for(i=0;i<10;i++)n for(j=0;j<10;j++)n if(a[i]
C 语言问题,求大神指导
求大神分析,代码如下:rnrn[code=c]rn#include rn#include rn#include rnrnvoid input(int *,int);rnvoid sort(int *,int);rnvoid output(int *,int);rnint main()rnrn int *p;rn int len,i;rnrn printf("请输入长度:\nlenth = ");rnrn scanf_s("%d",&len);rn rn p = (int *)malloc(len * sizeof(int));rnrn //input(p,len);rnrn for(i=0;i *(pArr+i+1))rn rn k = *(pArr+i);rn *(pArr+i) = *(pArr+i+1);rn *(pArr+i+1) = k;rn rn rn rnrnrnrnrnvoid output(int * p,int len)rnrn int i;rnrn for(i=0;i
C#调用c的dll问题
函数原型:int WINAPI rf_anticoll(unsigned short icdev, rn unsigned char bcnt,rn unsigned char *pSnr,rn unsigned char *pLen)rn参数:icdev:[IN] 通讯设备标识符rn bcnt: [IN] 取值4rn pSnr: [OUT]返回的卡序列号rn pLen: [OUT]返回序列号的长度rn返回:成功返回0rn我在c#中应如何定义入口参数?rnpublic static extern int rf_anticoll(int dev, byte bcnt, out string pSnr, out int len)rn这个不行。报错:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
C#调用C Dll的问题
在C#中调用C写的Dll,rn函数原型为 void GetPic(char * szCode, BYTE * pPicture)rnrn用C#建个窗口应用程序rnrn [DllImport("PictureGenerator.dll", EntryPoint = "GetPic")]rn static extern void GetPic(string szCode, byte[] pPicture);rnrnrn private void Form1_Load(object sender, EventArgs e)rn rn rn Byte[] pPic = new Byte[9000];rn GetPic("test", pPic);rn rnrn这样是可以调用的。rnrn但是,同样的代码,放在WEB中就不行了。在运行到GetPic时就会出错。说是 rnNullReferenceException: 未将对象引用设置到对象的实例rn这是为什么啊,窗口程序的与WEB有什么不同么。
求大神指导,C#任务栏的问题
编了一个程序,但是不清楚如何修改任务栏上图片和文字内容,求大神指导下~
求大神帮忙!传参问题!!
主函数中的一部分代码:rnfinal LinearLayout ll3 = (LinearLayout) findViewById(R.id.linechart3);rnLinearLayout tl3 = (LinearLayout) findViewById(R.id.rainll);rnrnfinal Rain geomark3 = (Rain) findViewById(R.id.geomark_view3);rntl3.setOnClickListener(new OnClickListener() rnrn @Overridern public void onClick(View v) rn // TODO Auto-generated method stubrn if (ll3.getVisibility() == 0) rn ll3.setVisibility(View.GONE);rn geomark3.setVisibility(View.GONE);rn else rn ll3.setVisibility(View.VISIBLE);rn geomark3.setVisibility(View.VISIBLE);rn rnrn rn );rnrn我想传参给Rain rnRain是public class Rain extends View 这种形式的 该采用什么形式传 在哪里传哪里接呢rnrn我打算画一个折线图,画图时需要获取用户名等数据以便访问数据库里相应的表
c中如何调用dll,在线等
希望给的具体一点,3ks
c中调用delphi的dll 在线等
一个java的b/s项目rn在java中调用dll,dll要完成引用一个ocx进行文件转化,rndll为delphi所写,请问怎么才能引用这个delphi做的dllrnrn用别的方式处理也可以rn
关于C#里面的结构体指针,在线等!!!
这几天在用C#里面的指针与结构体,感觉是N复杂啊,下面是我写的一个程序:rn[code=C#] rn unsafe public struct node rn rn public int i,j;rn public node* next;rn rn unsafe public node* f = null, r = null; //用于记录存取节点的链接队列的首尾指针rn unsafe public void insert (int k,int l)rn rn node newNode=new node();rn newNode.i = k;rn newNode.j = l;rn newNode.next = null;rn if (f == null)rn rn f = &newNode;rn rn else rn rn r->next = &newNode;rn rn r = &newNode;rn rn unsafe public void procNode(byte* ptr2) rn rn Bitmap newBp;rn insert(100, 100);rn int temp=f->i; ///这句就是出问题的地方,本来应该是100的,但为啥得到的是0?!!!~~~rn rn[/code]rn 请大家给我看看啊,我现在很急,在线等!!!~~~
C#调用dll返回值错误问题 (在线等~~)
我调用了一个DLLrn[DllImport("genTac.dll")]rnpublic static extern int genTac(int gICDevId, byte[] keybuf, byte[] databuf, byte[] tacbuf);rnrn这是引用的地方rnbyte[] tacbuf = new byte[300];rnrn string strKeyElements = "1509021410000025";rn stringstr DataStream = "001509021400000002250002011002000004229000000035002008082610502510000025";rnrnrn Byte[] keybuf = Encoding.GetEncoding("gb2312").GetBytes(strKeyElements);rn Byte[] databuf = Encoding.GetEncoding("gb2312").GetBytes(strDataStream); rnrnrn int sl = genTac(icdev, keybuf, databuf, tacbuf);rnSl 本应该为 0 而现在 确不是 同样的代码在vs2005上可以运行,但是vs2003就不行 rn请大家帮帮忙
C#调用Dephi的DLL疑问,在线等
function GetVcell_Index(Devicechannum,IndexSs:Integer;var mV:widestring):Integer;stdcall;rn 传递参数:Devicechannum: 通道号 (只有一个通道,这里可以传1)rn IndexSs:第几串rn mV:(回传值) rn 返回参数:=0 表明不成功rn =1 表明成功rnrn这是他的调用函数,那么我C#调用该如何调用,因为这个包含了2个返回值,就蒙了。。rnC#大致应该如下吧?rn [DllImport("UserDll.dll", CallingConvention = CallingConvention.StdCall)]rn // public static extern int GetVcell_Index(string OrignStr, string Key, int NewStr);rn以上仅供参考,能力有限,还请大虾帮忙!
(在线等)C#调用C++的dll
int __stdcall HS_DES(int op, char *key, char *inblock, char *outblock, int len);rnrn我要在C#中使用C++中的一个方法,封装在 XX.dll中。rn如何调用??rnrn我是这样写的 rnusing System.Runtime.InteropServices;rnrn[DllImport("XX.dll", EntryPoint = "HS_DES")]rn public static extern string HS_DES(int op, string key, string inblock, string outblock, int len);rnrn提示无法加载,我不清楚C++中的指针类型如何在C#中表示。rnrn怎么写才是正确的 大家帮下忙。。。
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件