C#,C++Dll文件调用心得

C#下:

1.新建-->项目-->控制台应用程序:填写各种名称之后项目新建成功:一下为默认生成方式。

2.如下,在Program.cs中添加如下代码:

    

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
     const string path = "../NLPIR.dll";//设定dll的路径,如果是Debug运行,系统默认路径为E:\灵玖\公司组件\C#\ConsoleApplication1\ConsoleApplication1\bin\Debug

                                                     //  如果是Release运行,系统默认路径为E:\灵玖\公司组件\C#\ConsoleApplication1\ConsoleApplication1\bin\Release

                                                    //      "../NLPIR.dll"是指寻找bin目录中的NLPIR.dll文件

//对函数进行申明
[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_Init")]
public static extern bool NLPIR_Init(String sInitDirPath, int encoding,String sLicenceCode);

//特别注意,C语言的函数NLPIR_API const char * NLPIR_ParagraphProcess(const char *sParagraph,int bPOStagged=1);必须对应下面的申明
[DllImport(path, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Winapi, EntryPoint = "NLPIR_ParagraphProcess")]
public static extern IntPtr NLPIR_ParagraphProcess(String sParagraph, int bPOStagged);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_Exit")]
public static extern bool NLPIR_Exit();

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_ImportUserDict")]
public static extern int NLPIR_ImportUserDict(String sFilename);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_FileProcess")]
public static extern bool NLPIR_FileProcess(String sSrcFilename, String sDestFilename, int bPOStagged);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_FileProcessEx")]
public static extern bool NLPIR_FileProcessEx(String sSrcFilename, String sDestFilename);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_GetParagraphProcessAWordCount")]
static extern int NLPIR_GetParagraphProcessAWordCount(String sParagraph);
//NLPIR_GetParagraphProcessAWordCount
[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_AddUserWord")]
static extern int NLPIR_AddUserWord(String sWord);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_SaveTheUsrDic")]
static extern int NLPIR_SaveTheUsrDic();

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_DelUsrWord")]
static extern int NLPIR_DelUsrWord(String sWord);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_NWI_Start")]
static extern bool NLPIR_NWI_Start();

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_NWI_Complete")]
static extern bool NLPIR_NWI_Complete();

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_NWI_AddFile")]
static extern bool NLPIR_NWI_AddFile(String sText);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_NWI_AddMem")]
static extern bool NLPIR_NWI_AddMem(String sText);

[DllImport(path, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Winapi, EntryPoint = "NLPIR_NWI_GetResult")]
public static extern IntPtr NLPIR_NWI_GetResult(bool bWeightOut);

[DllImport(path, CharSet = CharSet.Ansi, EntryPoint = "NLPIR_NWI_Result2UserDict")]
static extern uint NLPIR_NWI_Result2UserDict();
static void Main(string[] args)
{
if (!NLPIR_Init("../", 0,"0"))//组件Data目录的存放路径,"../"同样是指bin目录
{
System.Console.WriteLine("失败");
}
else
{
System.Console.WriteLine("成功");
}
Console.ReadLine();
}
}
}

可能出现的错误及解决方法:

     错误1: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

     解决方法: 在“解决方案资源管理器”里,右键该项目点击属性。在属性窗口里选择“生成”——“目标平台”下拉里选中“X86"或者"X64"即可

 C++下调用:

   1.文件-->新建-->空项目(Visual C++):填入名称等必填项:

    2.把NLPIR.dll  NLPIR.lib  Data目录复制到项目根目录下;

   3.右键项目名称-->添加-->新建项-->C++文件(.cpp)

   4.在新建文件中编写如下代码:

       

#include "../NLPIR.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<iostream>
#pragma comment(lib, "../NLPIR.lib")//调用关键代码,要确保lib和dll都存在
int main(void)
{
if(NLPIR_Init("../",1,"1"))
{
printf("success-----\n");
}else
{
printf("failed-----\n");
}
printf("--------\n");
system("pause");
}

这样运行Debug和Release都可以找到相对应大文件

posted @ 2014-09-03 16:13  风未馨  阅读(589)  评论(0编辑  收藏  举报