I have code for Enable or disable the comport in device manager through vb.net program in windows xp . but that code is not working in windows 7.
Is there any common function for both os.

Dani AI

Generated

Brief expert summary tied to the thread:

The two root causes in this thread are process elevation and platform/structure-size mismatches. As suggested, the process that calls SetupDi* must run elevated (manifest or explicitly elevated process). (learn.microsoft.com)
SP_DEVINFO_DATA.cbSize must be the correct size for the running bitness; hardcoded values (28/32) will fail on the wrong target and cause ERROR_INVALID_PARAMETER. Use a platform-aware size calculation rather than magic numbers. (learn.microsoft.com)

Practical troubleshooting checklist (apply in this order):

  1. Confirm the process is elevated (manifest or run as admin). (learn.microsoft.com)
  2. Match build target to OS (AnyCPU without Prefer32Bit or explicit x64) — a 32-bit process can trigger ERROR_IN_WOW64 for certain SetupDi calls. (learn.microsoft.com)
  3. Ensure class-install parameters are set (SP_PROPCHANGE_PARAMS) and call SetupDiSetClassInstallParams before calling the class installer or default handler. (learn.microsoft.com)
  4. When SetupDiCallClassInstaller returns False, get the Win32 error and a message for diagnosis:
' portable way to set SP_DEVINFO_DATA.cbSize
DeviceInfoData.cbSize = Runtime.InteropServices.Marshal.SizeOf(GetType(SP_DEVINFO_DATA))
' after a failing SetupDiCallClassInstaller:
Dim err As Integer = Runtime.InteropServices.Marshal.GetLastWin32Error()
Dim msg As String = New System.ComponentModel.Win32Exception(err).Message

Use the numeric error and message when logging or posting follow-ups; it identifies bitness issues, invalid parameters, or permission problems quickly. (learn.microsoft.com)

Recommended Answers

All 6 Replies

The attached code is written in C#. This would probably be better placed in the C# forum.

The issue is that you need to run as administrator.

Add an Application Manifest File: (to "HW_Lib_Test")

  • Click "Project"
  • Select "Add New Item"
  • Select "Application Manifest File"
  • Click "Add"

Then (in app.manifest) change from:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

To:

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

In "HardwareHelperLib" in "GetAll", add HWList.Sort();, because it is annoying to look through all the output if it is not sorted.

          ...
HWList.Add(DeviceName.ToString());
HWList.Sort(); //added
          ...

Then in "HardwareHelperLib", in "ChangeIt", you have the following:

catch (Exception ex)
{
   return false;
}

It is better to print out some sort of exception message or log the error message to a file (in my opinion).

Sir i have changed but it's wont work the HardwareHelperLib dll

It works for me. You need to provide more details. Also, your xp is most likely 32-bit. What version of win 7 are you using? Check to see if you are compiling for x86 or any cpu.

bool rstl2 = Native.SetupDiCallClassInstaller(Native.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData);
This line shows false in my system.
I am using win7 64 bit

In addition to the above, in "HardwareHelperLib" change from:

DeviceInfoData.cbSize = 28;

To:

//for 32-bit, IntPtr.Size = 4
//for 64-bit, IntPtr.Size = 8
if (IntPtr.Size == 4)
{
    DeviceInfoData.cbSize = 28;
}//if
else if (IntPtr.Size == 8)
{
    DeviceInfoData.cbSize = 32;
}//if

Note: There are two occurrences of the above (one in "SetDeviceState", and another in "GetAll").

Also, ensure that you are compiling for "Any CPU".

Resources:

SP_DEVINFO_DATA (Structures)

...for 32 bit SP_DEVINFO_DATA.cbSize=28, for 64Bit SP_DEVINFO_DATA.cbSize=(28+4)=32...

How to know in run time if i'm on x86 or x64 mode in c#

...You can check whether IntPtr.Size is 4 or 8...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.