- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 C# 项目中包含 DLL(用 C 编写)时遇到问题,希望您能帮助我。在我的研究中,我发现了 DLLImport,但我不确定 C 变量转换为哪些类型...
我要导入的 DLL 中的 C 方法如下:
int lou_translateString (
const char * tableList,
const widechar * inbuf,
int *inlen,
widechar *outbuf,
int *outlen,
char *typeform,
char *spacing,
int mode);
这是我试过的 DLLImport:
[DllImport("liblouis-2.dll", EntryPoint = "lou_translateString", CharSet = CharSet.Ansi, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern int translate([MarshalAs(UnmanagedType.LPStr)]
String tableList,
String inbuf,
int inlen,
String outbuf,
int outlen,
String typeform,
String spacing,
int mode);
但是当我尝试调用我得到的方法时:
MarshallDirectiveException无法编码“返回值”:无效的托管/非托管类型组合(Int32/UInt32必须搭配I4或U4)
任何人都可以帮忙解决这个异常(exception)吗?我很确定它与方法中的参数类型有关,但我不知道如何正确声明它们。任何帮助将不胜感激!
提前致谢亚历克斯
添加了简短的文档以进行说明
This function takes a string of 16-bit Unicode characters in inbuf and translates it into a string of 16-bit characters in outbuf.
The tableList parameter points to a list of translation tables separated by commas.
Note that both the *inlen and *outlen parameters are pointers to integers. When the function is called, these integers contain the maximum input and output lengths, respectively. When it returns, they are set to the actual lengths used.
The typeform parameter is used to indicate italic type, boldface type, computer braille, etc. It is a string of characters with the same length as the input buffer pointed to by *inbuf. However, it is used to pass back character-by-character results, so enough space must be provided to match the *outlen parameter. Each character indicates the typeform of the corresponding character in the input buffer. The values are as follows: 0 plain-text; 1 italic; 2 bold; 4 underline; 8 computer braille. These values can be added for multiple emphasis. If this parameter is NULL, no checking for type forms is done. In addition, if this parameter is not NULL, it is set on return to have an 8 at every position corresponding to a character in outbuf which was defined to have a dot representation containing dot 7, dot 8 or both, and to 0 otherwise.
The spacing parameter is used to indicate differences in spacing between the input string and the translated output string. It is also of the same length as the string pointed to by *inbuf. If this parameter is NULL, no spacing information is computed.
The mode parameter specifies how the translation should be done.
The function returns 1 if no errors were encountered and 0 if a complete translation could not be done.
这是我为了测试 Main 中的函数而写的:
StringBuilder outbuf = new StringBuilder("Test", 30);
int inlength = 100;
int louint = 0;
int outlength = 100;
String inbuf = "test";
louint = lou_translateString("de-de-g1.ctb", inbuf, inlen: ref inlength, outbuf: outbuf, outlen: ref outlength, typeform: null, spacing: null, mode: 8);
但是最后原来的outbuf根本没有变。你说参数对不对?我是否必须以某种方式修改 outbuf 参数,因为它是一个 StringBuilder 而不仅仅是一个字符串 ( outbuf.Replace() )?
最佳答案
返回值 MarshalAs
是错误的。返回值是 int
而不是字符串。这解释了错误,但还有更多。您的 pinvoke 声明的其余部分有错误。我会这样做:
[DllImport("liblouis-2.dll", CharSet = CharSet.Unicode)]
public static extern int lou_translateString(
[MarshalAs(UnmanagedType.LPStr)]
string tableList,
string inbuf,
ref int inlen,
StringBuilder outbuf,
ref int outlen,
[MarshalAs(UnmanagedType.LPStr)]
StringBuilder typeform,
[MarshalAs(UnmanagedType.LPStr)]
StringBuilder spacing,
int mode
);
从函数中编码出来的文本缓冲区必须声明为 StringBuilder
。你还有其他一些错误。将 int*
翻译成 ref int
。
为了调用该函数,您需要创建一个新的 StringBuilder
实例以作为 outbuf
参数传递。您必须计算出缓冲区需要多大。我假设该函数的文档会告诉您如何执行此操作。至少对于初始测试,您可以为 typeform
和 spacing
传递 null
。
关于c# - C Dll 导入在 C# 中引发 Marshall 指令异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20857649/
问题很简单:我正在寻找一种优雅的使用方式 CompletableFuture#exceptionally与 CompletableFuture#supplyAsync 一起.这是行不通的: priva
对于 Web 服务,我们通常使用 maven-jaxb2-plugin 生成 java bean,并在 Spring 中使用 JAXB2 编码。我想知道如何处理 WSDL/XSD 中声明的(SOAP-
这个问题已经有答案了: Array index out of bound behavior (10 个回答) 已关闭 8 年前。 我对下面的 C 代码感到好奇 int main(){
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
嘿 我尝试将 java 程序连接到 REST API。 使用相同的代码部分,我在 Java 6 中遇到了 Java 异常,并且在 Java 8 中运行良好。 环境相同: 信任 机器 unix 用户 代
我正在尝试使用 Flume 和 Hive 进行 Twitter 分析。为了从 twitter 获取推文,我在 flume.conf 文件中设置了所有必需的参数(consumerKey、consumer
我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台
假设我使用 blur() 事件验证了电子邮件 ID,我正在这样做: $('#email').blur(function(){ //make ajax call , check if dupli
我这样做是为了从 C 代码调用非托管函数。 pCallback 是一个函数指针,因此在托管端是一个委托(delegate)。 [DllImport("MyDLL.dll")] public stati
为什么这段代码是正确的: try { } catch(ArrayOutOfBoundsException e) {} 这是错误的: try { } catch(IOException e) {} 这段
我遇到了以下问题:有导出函数的DLL。 代码示例如下:[动态链接库] __declspec(dllexport) int openDevice(int,void**) [应用] 开发者.h: __de
从其他线程,我知道我们不应该在析构函数中抛出异常!但是对于下面的例子,它确实有效。这是否意味着我们只能在一个实例的析构函数中抛出异常?我们应该如何理解这个代码示例! #include using n
为什么需要异常 引出 public static void main(String[
1. Java的异常机制 Throwable类是Java异常类型的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,他才是一个异常对象,才能被异常处理机制识别。JDK中内
我是 Python 的新手,我对某种异常方法的实现有疑问。这是代码(缩写): class OurException(Exception): """User defined Exception"
我已经创建了以下模式来表示用户和一组线程之间的关联,这些线程按他们的最后一条消息排序(用户已经阅读了哪些线程,哪些没有): CREATE TABLE table(user_id bigint, mes
我正在使用 Python 编写一个简单的自动化脚本,它可能会在多个位置引发异常。在他们每个人中,我都想记录一条特定的消息并退出程序。为此,我在捕获异常并处理它(执行特定的日志记录操作等)后引发 Sys
谁能解释一下为什么这会导致错误: let xs = [| "Mary"; "Mungo"; "Midge" |] Array.iter printfn xs 虽然不是这样: Array.iter pr
在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @With 和一个 @Check 注释。 断开连接后,一切正常。连接后,每次加载页面(任何页面,无论
我尝试连接到 azure 表存储并添加一个对象。它在本地主机上工作得很好,但是在我使用的服务器上我得到以下异常及其内部异常: Exception of type 'Microsoft.Wind
我是一名优秀的程序员,十分优秀!