TransCAD和TransModeler软件具有一套完整的编程接口,除了包含一套GIS和交通模型应用开发工具库(GISDK),还包括微软的.NET(VC,VB,C#,JAVA等)、COM和Python编程接口。软件的基础算法基本上是C/C++代码完成的,以实现高效运算,而界面通常使用GISDK开发。
如果用户使用自己的算法开发了某个模型或者应用程序,希望在TransCAD或TransModeler软件中集成使用,则可以做成动态链接库供GISDK调用。本文介绍C/C++和C#的DLL动态链接库调用方法。
1、调用C/C++的DLL动态链接库
调用C/C++的DLL动态链接库可采用LoadLibrary()函数或者CallDllFunction()函数完成:
dllhandle= LoadLibrary(string dll_name [, array options])
ret_val = CallDllFunction(string dll_name, stringfunc_name, string ret_type, array args [, array options])
假设C/C++要导出的函数如下:
void GISDKTest(double *ID, long no_ID){
for (longi=0;i
// for (long i=1;i<=no_ID;i++) 不对,应该从0开始
{
ID[i]=ID[i]*ID[i];
}
}
假定C/C++编译的动态链接库文件为“CLDLL.dll”,则GISDK调用的方法如下:
Macro "DLLT"
DLLFunction=LoadLibrary("D:\\CLDLL.dll")
no_ID=5
dim ID[no_ID]
for i=1 to 5 do
ID[i]=2*i
end
args = {
{ "double*", ID },
{ "long", no_ID }
}
opts.FastCall ="True"
// opts.[ReturnType] = "double"
// 返回值类型
// fastcall
ret =DLLFunction.GISDKTest(args, opts)
// cdecall
// Ret = CallDllFunction("D:\\CLDLL.dll","GISDKTest", , args)
ShowArray(ID)
EndMacro
下面是使用LoadLibrary()函数调用windows的dll动态链接库例子:
// You can use this to hook into Windows API. Take for anexample:
// int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType)
// declared in user32.dll. You would actually call theMessageBoxA version with
hDll = LoadLibrary("user32.dll")
args = {{"long",0},{"char *","HelloWorld"},{"char *","My Title"},{"long",0}}
opts.StdCall = "True"
opts.[Return Type] = "long"
ret = hDll.MessageBoxA(args, opts)
// Initialize an string
str = null
for i = 1 to 200 do
str = str + " "
end
// Windows API uses StdCall
opts = {{"StdCall", "True"}}
// Call a function in kernel32.dll
Ret = CallDllFunction("kernel32", "GetSystemDirectoryA","int", {str, 200}, opts)
ShowArray({Ret, str})
2、调用C#的DLL动态链接库
调用C#的DLL动态链接库可采用创建托管对象CreateManagedObject ()函数完成:
managedobject = CreateManagedObject(stringassemblyName, string typeName, array parameters [, array options])
假设C#要导出的函数如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace csdlltest
{
public class tts
{
public int test(int i, int j)
{
return i + j;
}
public string testV(int []ii)
{
int tt = ii[0] + ii[1];
return tt.ToString();
}
}
}
假定C#编译的动态链接库文件为“tts.dll”,则GISDK调用的方法如下:
Macro "DLLT"
cs =CreateManagedObject("C:\\dll\\tts.dll", "csdlltest.tts ",)
sumv = cs.test(1, 2)
total = cs.testV({4,5})
//showmessage(I2s(sumv))
// showmessage(total)
EndMacro
TIPs:如果你使用了C/C++或者C#开发了一些有用的程序,例如处理GPS数据的地图匹配算法计算路网车速,处理公交IC卡数据程序等等,可以做成DLL供TransCAD和TransModeler软件调用,这样你就可以快速推广你的成果并且方便地和软件的GIS,数据库、展示以及交通模型结合使用了!你也可以开发成熟而具有市场价值的产品,供用户使用。
你有任何技术问题和软件购买需要,请联系我们:
客服微信号:YishuvInfo
微信公众号邮箱:TransCADTransModeler
Email:TransInfoTech@163.com
附录:调用函数说明
dllhandle= LoadLibrary(string dll_name [, array options])
Argument | Contents |
dll_name | The name of the Dynamic Link Library (DLL) |
Option | Type | Contents |
StdCall | Boolean | If "true" assumes the _stdcall calling convention, where arguments are passed on the stack and the called function is responsible for removing them off the stack; default is "false" |
FastCall | Boolean | If "true" assumes the _fastcall calling convention, where some arguments are passed in registers and the called function is responsible for popping other arguments off the stack; default is "false" |
Cdecl | Boolean | Assumes the _cdecl calling convention, where the arguments are passed on the stack and the and the calling function is responsible for removing them off the stack; defaultis "true" |
Return Type | String | Can be "double", "int" or "string" |
ret_val = CallDllFunction(string dll_name, stringfunc_name, string ret_type, array args [, array options])
Argument | Contents |
dll_name | The name of the Dynamic Link Library (DLL) |
func_name | The name of the function to call in the DLL |
ret_type | The type of value to return: "int", "double", "string" or null |
args | An array of arguments to pass to the function |
Option | Type | Contents |
StdCall | Boolean | If "true" assumes the _stdcall calling convention, where arguments are passed on the stack and the called function is responsible for removing them off the stack; default is "false" |
FastCall | Boolean | If "true" assumes the _fastcall calling convention, where some arguments are passed in registers and the called function is responsible for popping other arguments off the stack; default is "false" |
Cdecl | Boolean | Assumes the _cdecl calling convention, where the arguments are passed on the stack and the and the calling function is responsible for removing them off the stack; defaultis "true" |
managedobject = CreateManagedObject(stringassemblyName, string typeName, array parameters [, array options])
Argument | Contents |
assemblyName | The name of the assembly that contains the type for which you are creating an object (for assemblies stored in the system's Global Assembly Cache), or the path to the assembly DLLs |
typeName | The fully qualified type name, including its namespace |
parameters | An array of input parameters to be passed to the constructor for the object |
Option | Type | Contents |
Event Macro | string | Name of a macro callback that will be called for .NET events fired by the object |
Events | array | An array of strings for the names of the events for which the macro will be called |
Generic | array | An array of either strings or objects of type System.Type representing the type parameters for a generic object |