C++编写api一例

C++编写api一例 ; Com.Xp.VCT.def文件 ;------------------------------ ; Com.Xp.VCT.def : 定义模块参数。 LIBRARY "Com.Xp.VCT" EXPORTS ;VCT相关操作方法 OpenVCT @1 GetFileHandle @2 ReSetVCT @3 ReadNextLine @4 CloseVCT @5 Test @6 Input_Output @7 //Com.Xp.VCT.cpp 文件 #include "stdafx.h" #include "resource.h" #include "dllmain.h" #include "VCT.h" #include "IO.h" #include "stdio.h" #include <fcntl.h> #include <stdlib.h> #include <share.h> //------------------------------------------- VCT m_vct; STDAPI_(bool) OpenVCT(char* FileName) { bool rbc=false; rbc=m_vct.Open(FileName); return rbc;// }; STDAPI_(int) GetFileHandle() { int fh=m_vct.m_FileHandle; return fh; } STDAPI_(bool) ReSetVCT() { bool rbc=false; rbc=m_vct.ReSet(); return rbc;// }; STDAPI_(char *) ReadNextLine() { char* tt; tt=m_vct.ReadNextLine(); return tt;// }; STDAPI_(bool) CloseVCT() { bool rbc=false; rbc=m_vct.Close(); return rbc;// }; STDAPI_(char*) Test() { char* tt="hello world test"; return tt; }; STDAPI_(char*) Input_Output(char* msg) { char* tt=msg; int fh1 = _open(msg, _O_RDONLY ); //if(_sopen_s(&this->m_FileHandle,FileName,_O_RDONLY, _SH_DENYNO, 0)) if( fh1 != -1 ) { tt="opened file!"; _close( fh1 ); } else { tt="no open file"; } return tt; } //VCT.h文件 //------------------------------------- /****************************************** 功能:定义VCT头文件的读取方法的类VCT.h vp:hsg create date:2009-07-15 *******************************************/ class VCT { public: VCT(void); ~VCT(void); //打开VCT文件 bool Open(char* FileName); //重新设置指针到首地址 bool ReSet(); //读取下一行字符串 char* ReadNextLine(); char* ReadNextLine_1(); //查询 //bool Seek(); //关闭VCT文件 bool Close(); //属性 char* m_ErrorMessage; int m_FileHandle; }; //VCT.cpp文件 //------------------------------------- #include "StdAfx.h" #include <string> #include <fcntl.h> #include <stdlib.h> #include <share.h> #include "VCT.h" #include "io.h" #include "stdio.h" VCT::VCT(void) { } VCT::~VCT(void) { } //打开VCT文件 bool VCT::Open(char* FileName) { bool rbc=false; int fh1; fh1 = _open(FileName, _O_RDONLY ); //if(_sopen_s(&this->m_FileHandle,FileName,_O_RDONLY, _SH_DENYNO, 0)) if( fh1 != -1 ) { //this->m_FileHandle=open(FileName,1); m_FileHandle =fh1; rbc=true; } else { rbc=false; } return rbc; }; bool VCT::ReSet(){ return false; }; //读取下一行字符串 char* VCT::ReadNextLine() { int fh=m_FileHandle; char* ret=""; char* tt=""; char buffer[1]; unsigned int nbytes = 1, bytesread; if(!_eof(fh)) { //getline( bytesread=_read(fh,buffer,nbytes); if(bytesread>0) { tt=buffer;//new char[0]; } else { tt=""; } } return tt; }; //读取下一行字符串 char* VCT::ReadNextLine_1() { int fh=m_FileHandle; char* ret; char* tt; char buffer[1000]; unsigned int nbytes = 1000, bytesread; if(!_eof(fh)) { bytesread=_read(fh,buffer,nbytes); tt=buffer;//new char[0]; } return tt; }; //close //关闭VCT文件 bool VCT::Close() { bool rbc=false; if(m_FileHandle>0) { _close(m_FileHandle); rbc=true; } return rbc; }; .Net C# 测试代码 public class Com_Xp_VCT_API { //-------------X.dll---------------------- public const string XDLL = "Com.Xp.VCT.dll"; [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "OpenVCT")] public static extern bool OpenVCT(string FileName); //GetFileHandle [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "GetFileHandle")] public static extern long GetFileHandle(); [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "ReadNextLine")] public static extern string ReadNextLine(); [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "CloseVCT")] public static extern bool CloseVCT(); [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "ReSetVCT")] public static extern bool ReSetVCT(); [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "Test")] public static extern string Test(); [DllImport(XDLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "Input_Output")] public static extern string Input_Output(string msg); } private void button2_Click(object sender, EventArgs e) { string tt = Com_Xp_VCT_API.Test(); MessageBox.Show(tt.ToString(), "读取值Test"); tt = Com_Xp_VCT_API.Input_Output(Application.StartupPath+"//tt.txt"); MessageBox.Show(tt.ToString(), "读取值Input_Output"); bool rbc = Com_Xp_VCT_API.OpenVCT(Application.StartupPath + "//tt.txt"); MessageBox.Show(rbc.ToString(), "读取值OpenVCT"); long fh=Com_Xp_VCT_API.GetFileHandle(); MessageBox.Show(fh.ToString(), "读取值GetFileHandle"); string line = Com_Xp_VCT_API.ReadNextLine(); while (line != "") { this.textBox3.AppendText(line); line = Com_Xp_VCT_API.ReadNextLine(); } //MessageBox.Show(line, "读取值ReadNextLine"); Com_Xp_VCT_API.CloseVCT(); }

转载于:https://www.cnblogs.com/sqlite3/archive/2009/07/16/2566965.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值