简体   繁体   中英

cannot implicitly convert type void to int in C#

private string GetShortName(string LongName)
{
    string ShortFileName = new string('\0', 67);
    **int Result = Support.PInvoke.SafeNative.kernel32.GetShortPathName(ref LongName, ref ShortFileName, ((ShortFileName) is null) ? 0 : Convert.ToString(ShortFileName).Length);**
    ShortFileName = StringsHelper.GetFixedLengthString(ShortFileName, 67);
    return ShortFileName.Substring(0, Math.Min(Result, ShortFileName.Length));
}

The above line throws a error "cannot implicitly convert type void to int" GetShortPathName comes from Kernel32.dll that is wrapped in a class file as

 public static void GetShortPathName(ref string lpszLongPath, ref string lpszShortPath, int cchBuffer);

Is there is any alternative to get it in int result?

GetShortPathName should return a uint , not be void :

public static uint GetShortPathName(...)

If you don't have control over Support.PInvoke.SafeNative.kernel32 then you'll need to create a new wrapper around the kernel call that does return the uint .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM