尝试从 kernel32 导入 DllImport 时出现奇怪的异常

发布于 2024-08-15 20:20:27 字数 930 浏览 8 评论 0原文

我已经使用这个特殊功能几个月了,但是今天它停止工作了。我无法想象为什么,而且我不排除任何可能性,所以如果您有任何想法,请告诉我们!

我以这种方式加载函数:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉默的熊 2024-08-22 20:20:27

尝试使用 GetPrivateProfileString 代替,并使用大写 S。

Try GetPrivateProfileString instead, with a capital S.

分開簡單 2024-08-22 20:20:27

名称 GetPrivateProfileString 是 C++ 中定义的 GetPrivateProfileStringA(多字符版本)或 GetPrivateProfileStringW(unicode 版本)的别名。

该名称未在 DLL 中定义,因此您应该使用 DllImport 的 EntryPoint 字段来给出该函数的真实名称。使用 C# 中的 Unicode 版本。

[DllImport("kernel32", EntryPoint="GetPrivateProfileStringW")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

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#.

[DllImport("kernel32", EntryPoint="GetPrivateProfileStringW")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
惯饮孤独 2024-08-22 20:20:27

嗯...一些想法:

  • 您的进程的环境路径最近是否发生了更改,kernel32 可能不再位于其中?
  • 我认为导入不区分大小写,但您的大小写与 MSDN 不匹配(最后的“字符串”大写)。
  • 可能是 ANSI/Unicode 问题(就像 ANSI 问题消失了一样?) - 您可以尝试在 DllImport 属性上指定 CharSet=CharSet.Unicode,甚至可以使用 GetPrivateProfileStringW 准确指定 Unicode 版本。

Hmmm... A few thoughts:

  • Has your process's environment path changed recently where kernel32 might not be on it any more?
  • I don't think the import is case sensitive, but your casing doesn't match MSDN (the final 'string' is capitalized).
  • Could be an ANSI/Unicode issue (like the ANSI one went away?)- you could try specifying CharSet=CharSet.Unicode on the DllImport attribute, or even specify the Unicode version exactly with GetPrivateProfileStringW.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文