尝试从 kernel32 导入 DllImport 时出现奇怪的异常
我已经使用这个特殊功能几个月了,但是今天它停止工作了。我无法想象为什么,而且我不排除任何可能性,所以如果您有任何想法,请告诉我们!
我以这种方式加载函数:
[DllImport("kernel32")]
private static extern int GetPrivateProfilestring(string section, string key, string def, StringBuilder retVal, int size, string filePath);
然后我尝试以这种方式使用它:
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfilestring(Section, Key, "", temp, 255, strPath);
return temp.ToString();
正如所说,这工作了很长时间,但是从现在开始它会抛出此异常:
System.EntryPointNotFoundException:无法在 DLL“kernel32”中找到名为“GetPrivateProfilestring”的入口点
为什么会发生这种情况? dll 是否有可能被更改(通过 Windows 更新或其他方式)?也许只是找不到了,那么异常会有所不同吗?我知道这不太可能,但是正如我所说,我不排除任何可能性,因为这一直有效并且源代码没有改变...
更新:奇怪的是大写有帮助,它似乎是现在工作。但我还是很好奇为什么会发生这种事,为什么现在才发生?我可以向你保证,它已经工作了几个月了。
我有点害怕只是改变它并更新我们的软件,因为错误只发生在我的机器上(据我所知),但是旧方法已经在各种 PC 和配置上的生产环境中运行了 6 个多月。
I have been using this particular function for months now, however today it stopped working. I can't imagine why, and I'm ruling nothing out, so if you have any ideas please do tell!
I am loading function in such manner:
[DllImport("kernel32")]
private static extern int GetPrivateProfilestring(string section, string key, string def, StringBuilder retVal, int size, string filePath);
and then I try to use it this way:
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfilestring(Section, Key, "", temp, 255, strPath);
return temp.ToString();
as said this worked for ages, however from now on it throws this exception:
System.EntryPointNotFoundException: Unable to find an entry point named 'GetPrivateProfilestring' in DLL 'kernel32'
Why would this happen? Is it possible that the dll was changed (by windows update or something)? Maybe it just can't be found anymore, would the exception be different then? I know this is unlikely however as I've said Im ruling nothing out since this has always worked and the source code has not been changed...
Update: Oddly enough the capitalization helped, it seams to be working now. However I am still curious as to why has this happened, and why did it happen now? I can assure you that it worked for months now.
I am a bit afraid of just changing it and updating our software everywhere, since the error only occurred on my machine (as far as I know anyway), however the old method has been working in production on various PC's and configurations for over 6 months.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
GetPrivateProfileString
代替,并使用大写 S。Try
GetPrivateProfileString
instead, with a capital S.名称 GetPrivateProfileString 是 C++ 中定义的 GetPrivateProfileStringA(多字符版本)或 GetPrivateProfileStringW(unicode 版本)的别名。
该名称未在 DLL 中定义,因此您应该使用 DllImport 的 EntryPoint 字段来给出该函数的真实名称。使用 C# 中的 Unicode 版本。
The name GetPrivateProfileString is an alias for either GetPrivateProfileStringA (multi char version) or GetPrivateProfileStringW (unicode version) defined in C++.
This name is not defined in the DLL, so you should use the EntryPoint field of DllImport to give the true name of the function. Use the Unicode version from C#.
嗯...一些想法:
CharSet=CharSet.Unicode
,甚至可以使用 GetPrivateProfileStringW 准确指定 Unicode 版本。Hmmm... A few thoughts:
CharSet=CharSet.Unicode
on the DllImport attribute, or even specify the Unicode version exactly with GetPrivateProfileStringW.