1. 程式人生 > >如何生成和使用CLIPS動態連結庫

如何生成和使用CLIPS動態連結庫

使用VS2010建立DLL工程,如下圖:

建立過程中注意選擇DLL選項,和,空專案選項,如下圖:

將CLIPS-core原始碼拷貝到VS工程下,並匯入工程,新建三個檔案,命名分別為:“CLIPSWin32defStr.h”、“DllCLIPS.c”、“DllCLIPS.h”,如下圖:

“DllCLIPS.h”用來宣告要預留介面的函式,其中#include "CLIPS/clips.h"是為了引入函式執行過程中需要的標頭檔案,內容和截圖如下:

#include "CLIPS/clips.h"

#ifndef _DLLCLIPS_H_
#define _DLLCLIPS_H_

__declspec (dllexport) void* myCreateEnvironment();
__declspec (dllexport) int myWatch(char *itemName);
__declspec (dllexport) int myLoad(char *fileName);
__declspec (dllexport) void* myAssertString(char *theString);
__declspec (dllexport) long long myRun(long long runLimit);
__declspec (dllexport) void* myFindDeftemplate(char *deftemplateName);
__declspec (dllexport) void* myGetNextFactInTemplate(void *theTemplate,void *factPtr);
__declspec (dllexport) void myFactSlotValue(void *theEnv,void *vTheFact,char *theSlotName,DATA_OBJECT *returnValue);

#endif

“DllCLIPS.c”用來書寫宣告函式的函式體,內容和截圖如下:

#include "DllCLIPS.h"

void* myCreateEnvironment()
{
    return CreateEnvironment();
}

int myWatch(char *itemName)
{
    return Watch(itemName);
}

int myLoad(char *fileName)
{
    return Load(fileName);
}

void* myAssertString(char *theString)
{
    return AssertString(theString);
}

long long myRun(long long runLimit)
{
    return Run(runLimit);
}

void* myFindDeftemplate(char *deftemplateName)
{
    return FindDeftemplate(deftemplateName);
}

void* myGetNextFactInTemplate(void *theTemplate,void *factPtr)
{
    return GetNextFactInTemplate(theTemplate,factPtr);
}

void myFactSlotValue(void *theEnv,void *vTheFact,char *theSlotName,DATA_OBJECT *returnValue)
{
    FactSlotValue(theEnv,vTheFact,theSlotName,returnValue);
}

“CLIPSWin32defStr.h”用來書寫函式執行過程中需要用到的巨集定義和結構體,內容和截圖如下:

#define SYMBOL                          2
#define AssertString(a) EnvAssertString(GetCurrentEnvironment(),a)
#define FindDeftemplate(a) EnvFindDeftemplate(GetCurrentEnvironment(),a)
#define GetNextFactInTemplate(a,b) EnvGetNextFactInTemplate(GetCurrentEnvironment(),a,b)
#define myValueToString(target) (((struct symbolHashNode *) (target))->contents)


struct dataObject
  {
   void *supplementalInfo;
   unsigned short type;
   void *value;
   long begin;
   long end;
   struct dataObject *next;
  };
typedef struct dataObject DATA_OBJECT;

struct symbolHashNode
  {
   struct symbolHashNode *next;
   long count;
   int depth;
   unsigned int permanent : 1;
   unsigned int markedEphemeral : 1;
   unsigned int neededSymbol : 1;
   unsigned int bucket : 29;
   char *contents;
  };

然後生成解決方案,如下圖:

“dllCLIPS.dll”、“dllCLIPS.lib”檔案就會生成在工程目錄下第一級Debug目錄下,然後將“DllCLIPS.h”標頭檔案中的dllexport修改為dllimport,內容和截圖如下:

#include "CLIPSWin32defStr.h"

#ifndef _DLLCLIPS_H_
#define _DLLCLIPS_H_


__declspec (dllimport) void* myCreateEnvironment();
__declspec (dllimport) int myWatch(char *itemName);
__declspec (dllimport) int myLoad(char *fileName);
__declspec (dllimport) void* myAssertString(char *theString);
__declspec (dllimport) long long myRun(long long runLimit);
__declspec (dllimport) void* myFindDeftemplate(char *deftemplateName);
__declspec (dllimport) void* myGetNextFactInTemplate(void *theTemplate,void *factPtr);
__declspec (dllimport) void myFactSlotValue(void *theEnv,void *vTheFact,char *theSlotName,DATA_OBJECT *returnValue);

#endif 

將“CLIPSWin32defStr.h”和上面三個檔案一併儲存,就可以使用了。