Note that there are some explanatory texts on larger screens.

plurals
  1. POc# FindFirstDevice Marshaling
    text
    copied!<p>I have a c# application running on winCE 6.0 version. I need to unload/reload the SD card driver at run time. I am attempting to do this via calling FindFirstDevice, then DeactivateDevice/ActivateDeviceEX calls. My problem is the FindFirstDevice() call always fails. I believe it to be a problem with the way I am marshalling the second parameter to it. Can anyone tell me what I am doing wrong? Here is the code:</p> <pre><code> [DllImport("coredll.dll", SetLastError = true)] public static extern int FindFirstDevice(DeviceSearchType searchType, IntPtr searchParam, ref DEVMGR_DEVICE_INFORMATION pdi); public bool MountSDCardDrive(string mRegPath) { const int INVALID_HANDLE_VALUE = -1; int handle = INVALID_HANDLE_VALUE; DeviceSearchType searchType = DeviceSearchType.DeviceSearchByDeviceName; DEVMGR_DEVICE_INFORMATION di = new DEVMGR_DEVICE_INFORMATION(); di.dwSize = (uint)Marshal.SizeOf(typeof(DEVMGR_DEVICE_INFORMATION)); string searchParamString = "*"; IntPtr searchParam = Marshal.AllocHGlobal(searchParamString.Length); Marshal.StructureToPtr(searchParamString, searchParam, false); handle = FindFirstDevice(searchType, searchParam, ref di); if (handle == INVALID_HANDLE_VALUE) { // Failure - print error int hFindFirstDeviceError = Marshal.GetLastWin32Error(); using (StreamWriter bw = new StreamWriter(File.Open(App.chipDebugFile, FileMode.Append))) { String iua = "DevDriverInterface: error from FindFirstDevice: " + hFindFirstDeviceError.ToString(); bw.WriteLine(iua); } return false; } ... (rest of code) </code></pre> <p>If I change the line <code>Marshal.StructureToPtr(searchParamString, searchParam, false);</code> to <code>searchParam = Marshal.StringToBSTR(searchParamString);</code>"I end up with error 1168 (ERROR_NOT_FOUND) instead of 18 (no more files). </p> <p>Note my intent is to use a <code>searchParamString</code> of "SDH1" when I get this working. I am currently using <code>searchParamString</code> of "*" in order to see something returned and rule out the particular string value.</p> <p>Thanks for any help you can give - Lynn</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload