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.

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.