c# - GetShortPathNameW cannot find file specified -


i'm trying short path name of path contains wide characters, here code using, throwing exception file not found. file exists, doing wrong?

using system; using system.collections.generic; using system.text; using system.runtime.interopservices; using system.componentmodel;  namespace getshortpathnamew_test {     class program     {         [dllimport("kernel32.dll", setlasterror = true)]         private static extern int getshortpathnamew(string pathname, system.text.stringbuilder shortname, int cbshortname);          static void main(string[] args)         {             system.text.stringbuilder new_path = new system.text.stringbuilder(1500);             int n = getshortpathnamew("\\\\?\\c:\\\\temp\\test1.txt", new_path, 1024);              if (n == 0)             {                 throw new win32exception(marshal.getlastwin32error());             }         }     } } 

you explicitly call unicode version getshortpathnamew, not specify correct parameters calling unicode version.

firstly, rename method getshortpathname. enough start working, without other changes. parameters still "ansi" strings, that's okay, since you're calling "ansi" implementation.

secondly, can add charset = charset.unicode dllimport attribute specify want unicode implementation. makes sure strings passed unicode strings. make work intended.

nowadays, there should no reason not call unicode implementations of functions, default dllimport cannot change without breaking backwards compatibility, needs specified every function.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -