views:

251

answers:

3

Hello!

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.

+4  A: 

Try GetPrivateProfileString instead, with a capital S.

Blindy
As stated in the update section of the question, this has indeed helped. However I can't understand how it has been working until now, if this was really a cause for the error.
Rekreativc
Maybe you had an `EntryPoint` field that overwrote the function name? I don't know what to say. You could try to look at old code that works and disassemble it to see how it was declared.
Blindy
A: 

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.
nitzmahone
+2  A: 

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);
cedrou
An entry point is not necessary in this case. See the remarks http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.charset.aspx: "Use [...] the CharSet enumeration to specify [...] which entry-point name to invoke (the exact name given or a name ending with "A" or "W"). The default enumeration member for C# and Visual Basic is CharSet.Ansi and the default enumeration member for C++ is CharSet.None, which is equivalent to CharSet.Ansi."
0xA3