473,806 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dhcpsapi.dll p/invoke

Hey Guys,

I've got a problem in trying to get the DhcpDeleteClien tInfo method in
the DHCP Server Management API working. I *really* think the issue
revolves around my DHCP_SEARCH_INF O_TYPE enum. Eventually what I want
to get done is a delete from the DHCP server by MAC Address. The code
below works for IPAddress, but not Name or MACAddress. both of those
attempts yield the same error: "87: the parameter is incorrect."

Have a look and tell me what you think:

[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_BINARY_DAT A
{
public int DataLength;
public IntPtr Data;
}

public enum DHCP_SEARCH_INF O_TYPE:int
{
DhcpClientIpAdd ress =0,
DhcpClientHardw areAddress=1,
DhcpClientName= 2
}

[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_SEARCH_INF O
{
public uint SearchType;
public UInt32 ClientIpAddress ;
public DHCP_BINARY_DAT A ClientMacAddres s;
[MarshalAs(Unman agedType.LPWStr )]public string ClientName;

};
[DllImport("dhcp sapi.dll", SetLastError = true, CharSet =
CharSet.Unicode )]
public static extern int DhcpDeleteClien tInfo(
string DHCPServer,
IntPtr searchInfo);
[DllImport("neta pi32.dll", SetLastError = true)]
public extern static int NetApiBufferFre e(IntPtr lpBuffer);

[DllImport("neta pi32.dll", SetLastError = true)]
public extern static int NetApiBufferAll ocate(int ByteCount,ref int
Buffer);


public static void DoDeletionByIP( string IPAddress)
{
string dhcpServer = "n.n.n.n";
DHCP_SEARCH_INF O SearchInfo = new DHCP_SEARCH_INF O();
SearchInfo.Sear chType =
(int)DHCP_SEARC H_INFO_TYPE.Dhc pClientIpAddres s;
char [] dot = ".".ToCharArray ();
char[] zero = "0".ToCharArray ();
string [] Octets= IPAddress.Split (dot[0]);
string HexIP = "";

foreach(string Octet in Octets)
{
HexIP += Convert.ToStrin g(Convert.ToInt 16(Octet), 16).PadLeft(2,
zero[0]);
}


SearchInfo.Clie ntIpAddress = Convert.ToUInt3 2(HexIP, 16);

int bufptr = 0;
int StructSize;
StructSize = Marshal.SizeOf( SearchInfo);
NetApiBufferAll ocate(StructSiz e,ref bufptr);
Marshal.Structu reToPtr(SearchI nfo, (IntPtr)bufptr, true);

int result = DhcpDeleteClien tInfo(dhcpServe r, (IntPtr)bufptr) ;

NetApiBufferFre e((IntPtr)bufpt r);
if(result == 0)
{
Console.WriteLi ne(IPAddress+": Deleted");
}
else
{
int code = 0;
unchecked
{
code = (int)result;
}
Win32Exception ex = new Win32Exception( code);
Console.WriteLi ne(ex.NativeErr orCode + ":" + ex.Message);

}

}
public static void DoDeletionByMAC (string MACAddress)
{
string dhcpServer = "n.n.n.n";
DHCP_SEARCH_INF O SearchInfo = new DHCP_SEARCH_INF O();
SearchInfo.Sear chType =
(int)DHCP_SEARC H_INFO_TYPE.Dhc pClientIpAddres s;

DHCP_BINARY_DAT A macAddress = new DHCP_BINARY_DAT A();
int discarded = 0;
byte [] macArray = HexEncoding.Get Bytes(MACAddres s, out discarded);
int size=0;
for(int i=0;i<macArray. Length;i++)
{
size += Marshal.SizeOf( macArray[i]);
}
IntPtr pnt = Marshal.AllocHG lobal(size);
Marshal.Copy(ma cArray, 0, pnt, macArray.Length );
macAddress.Data = pnt;
macAddress.Data Length= size;
SearchInfo.Clie ntMacAddress = macAddress;

int bufptr = 0;
int StructSize;
StructSize = Marshal.SizeOf( SearchInfo);
NetApiBufferAll ocate(StructSiz e,ref bufptr);
Marshal.Structu reToPtr(SearchI nfo, (IntPtr)bufptr, true);

int result = DhcpDeleteClien tInfo(dhcpServe r, (IntPtr)bufptr) ;

NetApiBufferFre e((IntPtr)bufpt r);
if(result == 0)
{
Console.WriteLi ne(MACAddress+" : Deleted");
}
else
{
int code = 0;
unchecked
{
code = (int)result;
}
Win32Exception ex = new Win32Exception( code);
Console.WriteLi ne(ex.NativeErr orCode + ":" + ex.Message);

}
}

public static void DoDeletionByNam e(string Name)
{
string dhcpServer = "n.n.n.n";
DHCP_SEARCH_INF O SearchInfo = new DHCP_SEARCH_INF O();
SearchInfo.Sear chType = (int)DHCP_SEARC H_INFO_TYPE.Dhc pClientName;
SearchInfo.Clie ntName = Name;
int bufptr = 0;
int StructSize;
StructSize = Marshal.SizeOf( SearchInfo);
NetApiBufferAll ocate(StructSiz e,ref bufptr);
Marshal.Structu reToPtr(SearchI nfo, (IntPtr)bufptr, true);

int result = DhcpDeleteClien tInfo(dhcpServe r, (IntPtr)bufptr) ;

NetApiBufferFre e((IntPtr)bufpt r);
if(result == 0)
{
Console.WriteLi ne(Name+": Deleted");
}
else
{
int code = 0;
unchecked
{
code = (int)result;
}
Win32Exception ex = new Win32Exception( code);
Console.WriteLi ne(ex.NativeErr orCode + ":" + ex.Message);

}

}

Any extra information would be greatly appreciated.

Thanks......

Apr 28 '06 #1
0 2826

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4111
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that has been made. Whenever I attempt to update my frmProgress, the events are put placed at the end of the thread's queue and executed after my algorithm is finished. Using the frmProgress.Label.Invoke method with delagates, I have heard would fix...
1
4874
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote function, which in turn fires an event that is caught by the form. The form then need to add a value passed in the event to a form object, however, when using the Invoke method, it just hangs.
2
2678
by: Tom | last post by:
Hi Everybod I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate for all of my controls in the Form and dont define one delegate for each control. Some thing like to have a switch case in that delegate fucntion and passing control name to it.. what is the impact of this method in comparison with...
5
4313
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the exe. The button clicking part is working fine, but I just cannot get the method call. Here is my program snippet. **************************************************************
14
7355
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values (and types), and with out parmeters. What I want to do is to support each method call with exception (http 404, soap exception, and other types of exceptions) and wrap it with try & catch (a lots of catch ;-)
23
2648
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new EventHandler(); Invoke(d, new object{sender, e}); } else {
6
39439
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k, string s) 2. MyDelegate MyDelegateVar = MyMethod 3. MyForm.Invoke (MyDelegateVar) Some questions:
2
4427
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
3
3242
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my code is: Public Class MyClass1 Implements System.ComponentModel.ISynchronizeInvoke Private _InvokeRequired As Boolean = False Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10371
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.