我正在编写 VB.net 并需要调用 API(c 语言 DLL)
我的测试示例代码如下
//Read Source File
char *SourceFilePath = "C:\\Docs\\Scandi\\attach\\verifyTest\\center.xml";
FILE *sourcefile= fopen(SourceFilePath, "rb");
if (!sourcefile)
{
printf("Error=%s\n", *SourceFilePath);
return;
}
fseek(sourcefile,0,SEEK_END);
long src_ch_len = ftell(sourcefile);
rewind(sourcefile);
unsigned char *src_ch =(unsigned char*)malloc(sizeof(char)*src_ch_len);
result = fread(src_ch,1,src_ch_len,sourcefile);
if(result!=src_ch_len)
{
printf("Reading Error=%s\n", *sourcefile);
return;
}
fclose(sourcefile);
//Read Data File
//Skip...
rc = BasicVerify(algorithm, data, dataLen, key, signature, signatureLen);
API函数定义
unsigned long verify(unsigned long algorithm, unsigned char *data, int dataLen,unsigned char *signature, int signatureLen, char *cerFile)
如何将 fopen(SourceFilePath, "rb") 和 fread(src_ch,1,src_ch_len,sourcefile) 转换为 VB.NET
谢谢
最佳答案
在 VB .NET 中使用模式 "rb"
的 fopen
的模拟似乎是 the FileStream
class使用 FileAccess.Read
模式。从那里你可以使用 FileStream.Read
method作为 fread
的类比。例如:
dim sourceFile as FileStream
sourceFile = new FileStream("C:\\Docs\\Scandi\\attach\\verifyTest\\center.xml", FileAccess.Read)
dim result as Integer
result = sourceFile.Read(array, 0, array.Length)
但是,从您的 C 代码中的 fseek
后跟 ftell
看来,您想将整个文件读入内存。这通常是不受欢迎的,因为一个文件的大小可能有好几千兆字节。如果您真的想这样做,请使用 File.ReadAllBytes
方法,例如:
dim src_ch as Byte()
src_ch = File.ReadAllBytes("C:\\Docs\\Scandi\\attach\\verifyTest\\center.xml")
关于c - 如何将 c 代码 fread 转换为 vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35098672/