In Delphi there is a function ExpandUNCFileName that takes in a filename and converts it into the UNC equivalent. It expands mapped drives and skips local and already expanded locations.
Samples
C:\Folder\Text.txt -> C:\Folder\Text.txt
L:\Folder\Sample.txt -> \\server\Folder1\Folder\Sample.txt Where L: is mapped to \\server\Folder1\
\\server\Folder\Sample.odf -> \server\Folder\Sample.odf
Is there a simple way to do this in C# or will I have to use windows api call WNetGetConnection and then manually check the ones that wouldn't get mapped?
Here's some C# code with a wrapper function LocalToUNC, which seems to work OK, though I haven't tested it extensively.
[DllImport("mpr.dll")]
static extern int WNetGetUniversalNameA(
string lpLocalPath, int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize
);
// I think max length for UNC is actually 32,767
static string LocalToUNC(string localPath, int maxLen = 2000)
{
IntPtr lpBuff;
// Allocate the memory
try
{
lpBuff = Marshal.AllocHGlobal(maxLen);
}
catch (OutOfMemoryException)
{
return null;
}
try
{
int res = WNetGetUniversalNameA(localPath, 1, lpBuff, ref maxLen);
if (res != 0)
return null;
// lpbuff is a structure, whose first element is a pointer to the UNC name (just going to be lpBuff + sizeof(int))
return Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(lpBuff));
}
catch (Exception)
{
return null;
}
finally
{
Marshal.FreeHGlobal(lpBuff);
}
}
P/Invoke WNetGetUniversalName().
I've done it modifying this code from www.pinvoke.net.
There is no built-in function in the BCL which will do the equivalent. I think the best option you have is pInvoking into WNetGetConnection as you suggested.
Try this code, is written in Delphi .Net
you must translate it to c #
function WNetGetUniversalName; external;
[SuppressUnmanagedCodeSecurity, DllImport(mpr, CharSet = CharSet.Ansi, SetLastError = True, EntryPoint = 'WNetGetUniversalNameA')]
function ExpandUNCFileName(const FileName: string): string;
function GetUniversalName(const FileName: string): string;
const
UNIVERSAL_NAME_INFO_LEVEL = 1;
var
Buffer: IntPtr;
BufSize: DWORD;
begin
Result := FileName;
BufSize := 1024;
Buffer := Marshal.AllocHGlobal(BufSize);
try
if WNetGetUniversalName(FileName, UNIVERSAL_NAME_INFO_LEVEL,
Buffer, BufSize) <> NO_ERROR then Exit;
Result := TUniversalNameInfo(Marshal.PtrToStructure(Buffer,
TypeOf(TUniversalNameInfo))).lpUniversalName;
finally
Marshal.FreeHGlobal(Buffer);
end;
end;
begin
Result :=System.IO.Path.GetFullPath(FileName);
if (Length(Result) >= 3) and (Result[2] = ':') and (Upcase(Result[1]) >= 'A')
and (Upcase(Result[1]) <= 'Z') then
Result := GetUniversalName(Result);
end;
Bye.
Related
I have a function in a C++ DLL that takes one input. I'm trying to have that input be used as an output to the C# call.
Here is my C++ function:
MYAPI int testStuff3(unsigned char* str)
{
printf("%s\n", str);
str = (unsigned char*)malloc(9);
str[0] = 'G';
str[1] = 'o';
str[2] = 'o';
str[3] = 'd';
str[4] = 'b';
str[5] = 'y';
str[6] = 'e';
str[7] = '!';
str[8] = '\0';
return 1;
}
Here is the C# code:
public class Program
{
[DllImport("NativeLib.dll")]
private static extern int testStuff3([In, Out] IntPtr str);
static void Main(string[] args)
{
IntPtr junk3 = IntPtr.Zero;
int ret = testStuff3(junk3);
Byte[] stuff3 = new byte[9];
Marshal.Copy(junk3, stuff3, 0, 9);
}
}
When the Marshal.Copy is called, it gives an error saying that the source (junk3) can not be null.
Will this not work, sending a null pointer to C++ DLL from C# and having the DLL allocate the memory and store something inside and return it to the caller? I want to keep it an IntPtr and not a StringBuilder because the data won't necessarily be a string in the final code. Just an unsigned char array in C++ and I want the IntPtr to point to it.
I've tried different variations of [In, Out], [Out], out and ref for the IntPtr passing.
Never ever allow memory allocations to cross a DLL boundary. That way lies madness, and/or Sparta.
(For the pedantic: you can allocate memory and then pass a pointer across, as long as you either pass ownership back to free it, or guarantee that the same allocator is used as part of a contract. But it's still something to avoid when possible.)
Typically to use a string output parameter you should pass a StringBuilder as the argument, setting its capacity to the maximum expected length. Then in the native code you simply fill this existing buffer.
See the "Fixed length string buffers" section here for an example.
Thanks for the help!
Here's what I ended up with.
C++ function:
MYAPI int testStuff4(wchar_t* str)
{
unsigned char* stuff = (unsigned char*)malloc(10);
stuff[0] = 'G';
stuff[1] = 'o';
stuff[2] = 'o';
stuff[3] = 'd';
stuff[4] = 'b';
stuff[5] = 'y';
stuff[6] = 'e';
stuff[7] = '!';
stuff[8] = '\0';
mbstowcs(str, (const char*)stuff, 1024);
free(stuff);
return 1;
}
C# function:
public class Program
{
[DllImport("NativeLib.dll")]
private static extern int testStuff4(IntPtr str);
static void Main(string[] args)
{
IntPtr junk4 = Marshal.AllocHGlobal(1024);
int ret = testStuff4(junk4);
string junkString = Marshal.PtrToStringUni(junk4);
Console.WriteLine(junkString);
Marshal.FreeHGlobal(junk4);
}
}
Your C++ function doesn’t modify the passed string. It allocates a new one with malloc, stores it in a local variable forgetting the passed value, then returns leaking the memory.
If for some reason you want to do manual marshalling, you probably want something like this (assuming this is for Windows):
MYAPI BOOL __stdcall testStuff3( char** pp )
{
if( nullptr == pp )
return FALSE; // null pointer
if( nullptr != *pp )
{ // Print & release an old string
printf( "%s\n", *pp );
CoTaskMemFree( *pp );
*pp = nullptr;
}
// Allocate a new one
const char* const str = CoTaskMemAlloc( 9 );
if( nullptr == str ) return FALSE;
strncpy( str, "Goodbye!", 9 );
*pp = str;
return TRUE;
}
C#:
public class Program
{
[DllImport( "NativeLib.dll" )]
private static extern bool testStuff3( [In, Out] ref IntPtr str );
static void Main( string[] args )
{
IntPtr ptr = IntPtr.Zero;
if( testStuff3( ref ptr ) )
{
Console.WriteLine( Marshal.PtrToStringAnsi( ptr ) );
Marshal.FreeCoTaskMem( ptr );
}
}
}
However, this is not something I recommend doing unless you have very good reasons. In most cases automatic marshalling is better. For C# -> C++ way it’s trivially simple, const char* or const wchar_t* in C++, string (with correct attributes) in C#. For C++ -> C# you can allocate a StringBuilder in C#, pass char* or wchar_t* to C++, and buffer length in another argument.
I'm trying to create a PInvoke for FbwfFindFirst and am struggling with the struct FbwfCacheDetail.
In short, I'm not sure how to marshal WCHAR fileName[1]; seeing as it's a variable length array and a non-null terminated.
Any help would be welcomed
Since the whole structure is of variable size, one way to do this is like this (I can't test it because I don't have this dll on my system):
string volume = "";
int size = 0;
// ask for whole structure size
FbwfFindFirst(volume, IntPtr.Zero, ref size); // this call should return ERROR_MORE_DATA which is ok
// allocate for the structure
var ptr = Marshal.AllocHGlobal(size);
try
{
FbwfFindFirst(volume, ptr, ref size); // should not return error
// get the easy part
var detail = Marshal.PtrToStructure<FbwfCacheDetail>(ptr);
// compute filename offset and get the string
// file name length is in bytes, per documentation
var fileName = Marshal.PtrToStringUni(ptr + Marshal.OffsetOf<FbwfCacheDetail>("fileName").ToInt32(), detail.fileNameLength / 2);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
[DllImport("fbwflib", CharSet = CharSet.Unicode)]
static extern int FbwfFindFirst(string volume, IntPtr cacheDetail, ref int size);
[StructLayout(LayoutKind.Sequential)]
struct FbwfCacheDetail
{
public int cacheSize;
public int openHandleCount;
public int fileNameLength;
byte fileName; // don't use this member
}
Simon Mourier's answer is 99% correct, and with normal APIs it would have definitely worked, but it appears as if this particular API doesn't follow "normal rules", whatever that might be ;). As such, I needed to modify a couple things and this is what worked for me:
const int ERROR_INSUFFICIENT_BUFFER = 122;
const int ERROR_MORE_DATA = 234;
var volume = "C:";
var fileName = string.Empty;
var size = 0;
while (true)
{
var ptr = Marshal.AllocHGlobal(size); // FbwfFindFirst fails if given IntPtr.Zero - regardless of what the value of size is.
try
{
var result = FbwfFindFirst(volume, ptr, ref size);
// Despite documentation saying otherwise, it can return either of these
if (result == ERROR_MORE_DATA || result == ERROR_INSUFFICIENT_BUFFER)
{
continue;
}
if (result != 0)
{
throw new Exception($"Failed with {result}");
}
// get the easy part
var detail = (FbwfCacheDetail) Marshal.PtrToStructure(ptr, typeof(FbwfCacheDetail));
// compute filename offset and get the string
// file name length is in bytes, per documentation
fileName = Marshal.PtrToStringUni(ptr + Marshal.OffsetOf(typeof(FbwfCacheDetail), "fileName").ToInt32(), detail.fileNameLength / 2);
break;
}
finally
{
Marshal.FreeHGlobal(ptr);
}
}
EDIT
Forgot to say that the pinvoke for FbwfFindFirst needs to be as follows, else it will return ERROR_INVALID_PARAMETER:
[DllImport("fbwflib.dll")]
public static extern uint FbwfFindFirst(
[MarshalAs(UnmanagedType.LPWStr)]
string volume,
IntPtr cacheDetail,
ref int size
);
What's the equivalent to Marshal.PtrToStringAnsi (C#) in C language ?
Because, I would like rewrite a function C# in C.
This is the C# function:
public int GetProcessName(uint processId, out string name)
{
IntPtr ptr = Marshal.AllocHGlobal((int)(0x211)); int result = -1;
result = getProcessName(processId, ptr);
name = String.Empty;
if(SUCCESS(result))
name = Marshal.PtrToStringAnsi(ptr);
Marshal.FreeHGlobal(ptr);
return result;
}
And actually my C function:
int
GetProcessName(uint processId, char *name)
{
HINSTANCE hLib = LoadLibrary("CCAPI.DLL");
__cGetProcessName getProcessName = (__cGetProcessName)GetProcAddress(hLib, "CCAPIGetProcessName");
int *ptr = malloc((int)(0x211));
int result = -1;
result = getProcessName(processId, ptr);
name = "";
if (SUCCESS(result))
{
name = /* ?? */;
}
free(ptr);
return (result);
}
In C#, Marshal.PtrToStringAnsi takes a pointer and returns a string.
In C, there is no string type and nothing equivalent out of the box. The closest you can have is a char* that points to an array of characters terminated with a null character ('\0'). By convention, we call this a "C string" (even though it's not a type of its own).
Thus, if you have a char*, you already have the C equivalent of the output of a Marshal.PtrToStringAnsi on that char* (as a IntPtr) in C#.
So this line:
name = Marshal.PtrToStringAnsi(ptr);
Would have been something like this:
name = ptr;
But since you seem to be freeing the ptr buffer at the end and you want the result in the externally provided buffer name, the correct alternative in C would actually be:
strcpy(name, ptr);
I have a C++ project in which I have to return some variables from C++ to C#.
These char variables are in the main program:
char test1[MAX_Q_LEN], test2[MAX_Q_LEN], test3[MAX_Q_LEN];
After I finish doing something with these variables in my C program, I have to return the values of these variables in a C# program.
ReturnChar.h
extern "C" RETURNCHAR_API TCHAR* __cdecl testString();
ReturnChar.cpp
extern "C" RETURNCHAR_API TCHAR* testString()
{
return ;
}
TestImport C#
static class TestImport
{
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr testString();
}
public partial class MainWindow : Window
{
public MainWindow()
{
try
{
InitializeComponent();
textBox1.Text = ReturnSomething()
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private static string ReturnSomething()
{
IntPtr t = TestImport.testString();
String result = Marshal.PtrToStringAuto(t);
}
I tried with the above approach but I am not able to find out how to return the above char values. Also, this should not be an independent function because the values shoud be fetched only after executing the main which will give the right values in these variables.
Any suggestions?
I will suggest a solution which would require you to change function signature to this:
extern "C" int __cdecl testString(char *output, int outputSize);
That is, pass an allocated buffer as the first argument to the function which will hold the output, and pass the size of the buffer as the second argument.
Note that I mention the return type of the function as int. It is because you could return the output actual size from the function, and the caller can interpret this value to confirm that outputSize value was large enough to hold the output string. For example, you could implement testString() as:
int testString(char *output, int outputSize)
{
std::string const & s = getString();
if ( s.size() <= outputSize )
{
std::strncpy(output, s.c_str(), s.size());
return s.size(); //return the actual size of output
}
else //means s.size() > outputSize, i.e outputSize is smaller than required!
{
std::strncpy(output, s.c_str(), outputSize);
return s.size(); //return what is required (the actual size of output)!
}
}
Then in C# code, do this:
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int testString(out StringBuilder output, int outputSize);
And call it as:
private static string ReturnSomething()
{
int bufferSize = 100;
StringBuilder buffer= new StringBuilder(bufferSize);
int outputSize = TestImport.testString(buffer, bufferSize);
if ( outputSize < bufferSize ) //output bufferSize was sufficient
{
return buffer.ToString();
}
else //output bufferSize was insufficient
{
//retry!
bufferSize = outputSize;
buffer = new StringBuilder(bufferSize); //reallocate!
outputSize = TestImport.testString(buffer, bufferSize);
if ( outputSize <= bufferSize )
return buffer.ToString();
else
{
throw new Exception("PANIC");
}
}
}
I'm not quite big c++ spec, but maybe to use bstrs in c++
_bstr_t text("saasas");
return text.Detach();
and for c# parameter
[MarshalAs(UnmanagedType.BStr)]
or to pass StringBuilder to your c++ func with some preallocated capacity
I've registered a window with RegisterDeviceNotification and can successfully recieve DEV_BROADCAST_DEVICEINTERFACE messages. However, the dbcc_name field in the returned struct is always empty. The struct I have is defined as such:
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(UnmanagedType.LPStr)]
public string dbcc_name;
}
And I'm using Marshal.PtrToStructure on the LParam of the WM_DEVICECHANGE message.
Should this be working?
Or even better... Is there an alternative way to get the name of a device upon connection?
EDIT (02/05/2010 20:56GMT):
I found out how to get the dbcc_name field to populate by doing this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)]
public string dbcc_name;
}
but I still need a way to get a "Friendly" name from what is int dbcc_name. It looks like the following:
\?\USB#VID_05AC&PID_1294&MI_00#0#{6bdd1fc6-810f-11d0-bec7-08002be2092f}
And I really just want it to say "Apple iPhone" (which is what the device is in this case).
Well, as noted above I found out how to get dbcc_name to populate correctly. I found that this was the easiest way to get the device name:
private static string GetDeviceName(DEV_BROADCAST_DEVICEINTERFACE dvi)
{
string[] Parts = dvi.dbcc_name.Split('#');
if (Parts.Length >= 3)
{
string DevType = Parts[0].Substring(Parts[0].IndexOf(#"?\") + 2);
string DeviceInstanceId = Parts[1];
string DeviceUniqueID = Parts[2];
string RegPath = #"SYSTEM\CurrentControlSet\Enum\" + DevType + "\\" + DeviceInstanceId + "\\" + DeviceUniqueID;
RegistryKey key = Registry.LocalMachine.OpenSubKey(RegPath);
if (key != null)
{
object result = key.GetValue("FriendlyName");
if (result != null)
return result.ToString();
result = key.GetValue("DeviceDesc");
if (result != null)
return result.ToString();
}
}
return String.Empty;
}
This information can also be acquired more formally through SetupAPI. Pass dbcc_name to SetupDiOpenDeviceInterface and get the friendly name with SetupDiGetDeviceRegistryProperty passing in SPDRP_FRIENDLYNAME.
Here's some Delphi code that will do it. (Sorry, you'll have to translate to C# independently).
function ConvertDbccNameToFriendlyName(aDeviceInterfaceDbccName : string) : string;
var
deviceInfoHandle : HDEVINFO;
deviceInfoData : SP_DEVINFO_DATA;
deviceInterfaceData : SP_DEVICE_INTERFACE_DATA;
deviceInstanceId : string;
memberIndex : Cardinal;
begin
result := '';
// Create a new empty "device info set"
deviceInfoHandle := SetupDiCreateDeviceInfoList(nil, 0);
if deviceInfoHandle <> INVALID_HANDLE_VALUE then
begin
try
// Add "aDeviceInterfaceDbccName" to the device info set
FillChar(deviceInterfaceData, SizeOf(deviceInterfaceData), 0);
deviceInterfaceData.cbSize := SizeOf(deviceInterfaceData);
if SetupDiOpenDeviceInterface(deviceInfoHandle, PChar(aDeviceInterfaceDbccName), 0, #deviceInterfaceData) then
begin
try
// iterate over the device info set
// (though I only expect it to contain one item)
memberIndex := 0;
while true do
begin
// get device info that corresponds to the next memberIndex
FillChar(deviceInfoData, SizeOf(deviceInfoData), 0);
deviceInfoData.cbSize := SizeOf(deviceInfoData);
if not SetupDiEnumDeviceInfo(deviceInfoHandle, memberIndex, deviceInfoData) then
begin
// The enumerator is exhausted when SetupDiEnumDeviceInfo returns false
break;
end
else
begin
Inc(memberIndex);
end;
// Get the friendly name for that device info
if TryGetDeviceFriendlyName(deviceInfoHandle, deviceInfoData, {out} friendlyName) then
begin
result := friendlyName;
break;
end;
end;
finally
SetupDiDeleteDeviceInterfaceData(deviceInfoHandle, deviceInterfaceData);
end;
end;
finally
SetupDiDestroyDeviceInfoList(deviceInfoHandle);
end;
end;
end;
function TryGetDeviceFriendlyName(
var aDeviceInfoHandle : HDEVINFO;
var aDeviceInfoData : SP_DEVINFO_DATA;
out aFriendlyName : string) : boolean;
var
valueBuffer : array of byte;
regProperty : Cardinal;
propertyRegDataType : DWord;
friendlyNameByteSize : Cardinal;
success : boolean;
begin
aFriendlyName := '';
result := false;
// Get the size of the friendly device name
regProperty := SPDRP_FRIENDLYNAME;
friendlyNameByteSize := 0;
SetupDiGetDeviceRegistryProperty(
aDeviceInfoHandle, // handle to device information set
aDeviceInfoData, // pointer to SP_DEVINFO_DATA structure
regProperty, // property to be retrieved
propertyRegDataType, // pointer to variable that receives the data type of the property
nil, // pointer to PropertyBuffer that receives the property
0, // size, in bytes, of the PropertyBuffer buffer.
friendlyNameByteSize); // pointer to variable that receives the required size of PropertyBuffer
// Prepare a buffer for the friendly device name (plus space for a null terminator)
SetLength(valueBuffer, friendlyNameByteSize + sizeof(char));
success := SetupDiGetDeviceRegistryProperty(
aDeviceInfoHandle,
aDeviceInfoData,
regProperty,
propertyRegDataType,
#valueBuffer[0],
friendlyNameByteSize,
friendlyNameByteSize);
if success then
begin
// Ensure that only 'friendlyNameByteSize' bytes are used.
// Ensure that the string is null-terminated.
PChar(#valueBuffer[friendlyNameByteSize])^ := char(0);
// Get the returned value as a string
aFriendlyName := StrPas(PChar(#valueBuffer[0]));
end;
result := success;
end;
Finally... if you need a way to uniquely identify a USB device (not what you asked for, but commonly this is also needed), look into SetupDiGetDeviceInstanceId.
It is likely you need to change this slightly
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[MarshalAs(UnmanagedType.LPStr)]
public StringBuilder dbcc_name;
}
Set the dbcc_size to 255, and construct the StringBuilder as shown below:
DEV_BROADCAST_DEVICEINTERFACE dbd = new DEV_BROADCAST_DEVICEINTERFACE;
dbd.dbcc_size = 255;
dbd.dbcc_name = new StringBuilder(dbd.dbcc_size);
Then pass that structure in, the value of dbcc_name should be populated.
Edit: after snicker's comment...I thought of this another way...
public struct DEV_BROADCAST_DEVICEINTERFACE
{
public int dbcc_size;
public int dbcc_devicetype;
public int dbcc_reserved;
public Guid dbcc_classguid;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = 255, ArraySubType = System.Runtime.InteropServices.UnmanagedType.LPArray)]
public string dbcc_name;
}
Set the dbcc_size to 255, and take it from there...
Edit#2: This is interesting...am not so sure now, I found this article that uses RegisterDeviceNotification on Codeproject and it uses a different way of RegisterDeviceNotification in that the struct is marshalled into a IntPtr and is used to call the API...