无法定位程序输入点 pSetupDiGetStrongNa假实村失故试和meForDriverNode 于动态连接库 SETUPAPI.dll 上。
问题补充说明:在用360驱动大师的时候出现这个窗口。点击确定后360驱动大师正常运行,但是扫描不到驱动
第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:
L360问答IBEXPORT_APIintmySum(inta,所intb){returna+b;}
C#意号阶军械转渐单导入定义:
publ集编标脸按号双斗icclassRefComm
{
[DllImport("LibEncrypt.dll",
E冲力简频除排甚混特点ntryPoint="mySum",
CharSet=**最农少叫被*.Auto,CallingConvention=CallingConvention.StdCall)]
publicstaticexternintmySum(inta,intb);
}
在坚轮调C#中调用测试:
intiSum=RefComm.mySum(,);
运行查看结果iSum为计缺5,调用正确。第一步试验完江啊础成局相我紧成,说明在C#中能够调用自件院群兵法定义的动态链接库函数。
第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函三数名),返回结果为国免字符串:
LIBEXPORT_APIchar*mySum(char*a,char*段杨而管亮b){sprintf(b,"%s",a);returna;}
C#导入定义:
publicclassRefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint="my真军女垂何获而仍Sum",
CharSet=***.Auto料,
CallingConvention=CallingConvention.StdCall)]
publicstaticexternst还ringmySum(stringa,stringb);
}
在C#中调用测试:
stringstrDest="";
stringstrTmp=RefComm.mySum("45",strDest);
运行查看结果strTmp为"45",但是strDest为空。我修改动态链接库实现,返回结果为串b:
LIBEXPORT_APIchar*mySum(char*a,char*b){sprintf(b,"%s",a)returnb;}
修改C#导入定义,将串b修改为ref方式:
publicclassRefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint="mySum",
CharSet=***.Auto,CallingConvention=CallingConvention.StdCall)]
publicstaticexternstringmySum(stringa,refstringb);
}
在C#中再调用测试:
stringstrDest="";
stringstrTmp=RefComm.mySum("45",refstrDest);
运行查看结果strTmp和strDest均不对,含不可见字符。再修改C#导入定义,将CharSet从Auto修改为Ansi:
publicclassRefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint="mySum",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
publicstaticexternstringmySum(stringa,stringb);
}
在C#中再调用测试:
stringstrDest="";
stringstrTmp=RefComm.mySum("45",refstrDest);
运行查看结果strTmp为"45",但是串strDest没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。再次修改C#导入定义,将串b修改为引用(ref):
publicclassRefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint="mySum",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
publicstaticexternstringmySum(stringa,refstringb);
}
运行时调用失败,不能继续执行。
第三步,修改动态链接库实现,将b修改为双重指针:
LIBEXPORT_APIchar*mySum(char*a,char**b){sprintf((*b),"%s",a);return*b;}
C#导入定义:
publicclassRefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint="mySum",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
publicstaticexternstringmySum(stringa,refstringb);
}
在C#中调用测试:
stringstrDest="";
stringstrTmp=RefComm.mySum("45",refstrDest);
运行查看结果strTmp和strDest均为"45",调用正确。第三步实现了函数出口参数正确输出结果。
第四步,修改动态链接库实现,实现整数参数的输出:
LIBEXPORT_APIintmySum(inta,intb,int*c){*c=a+b;return*c;}
C#导入的定义:
publicclassRefComm
{
[DllImport("LibEncrypt.dll",
EntryPoint="mySum",
CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
publicstaticexternintmySum(inta,intb,refintc);
}
在C#中调用测试:
intc=0;
intiSum=RefComm.mySum(,,refc);
运行查看结果iSum和c均为5,调用正确。
经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在C#定义导入,有此基础,很快我实现了变长加密函数在C#中的调用,至此目标实现。
三、结论
在C#中调用C++编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于C#的导入定义,则需要使用引用(ref)定义。
对于函数返回值,C#导入定义和C++动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意CharSet和CallingConvention参数,否则导致调用失败或结果异常。运行时,动态链接库放在C#程序的目录下即可,我这里是一个C#的动态链接库,两个动态链接库就在同一个目录下运行。
标签:pSetupDiGetStrongNa,假实,村失