Weird exception when trying to DllImport from kernel32
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.
Try GetPrivateProfileString
instead, with a capital S.
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);
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.
精彩评论