|
hi.
I new to C# and would like to know how i send an ARP packet to a computer using sendarp from iphlpapi.dll.
I saw this code somewhere on the web, and its what i need. but i have little idea how to use it:
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
I can see that a function is being build.
Can i just use it by doing this:
Int32 IP = Convert.ToDecimal(192.168.0.1);
SendARP(IP,0,0,IP.Length)
or how do i do it ?
|
|
|
|
|
Mmm, no, that won't work.. You can't create integer IP's from just using Convert.ToDecimal(). That would be too simple.
Also, you can't pass in a 0 for the 3rd parameter, it's a byte[] that's returned out as your data that you receive from the ARP I think (I haven't done any research, I'm just letting you know basic things that are wrong.)
What this all means is that you need to:
a) Declare a byte[] to use for the 3rd parameter. I'd reccomend using one that's got a decent length, like 256...declared as so: byte[] buffer = new byte[256]; (Note: You might not even need to declare a length, since you have that ref integer parameter that seems to tell you how much data you got out of the function - I'd try both ways)
b) for the 4th parameter, you need to declare an integer before calling the function, then pass that integer in as a ref parameter...that variable will hold the length of the actual output from the function (how much data it filled in the buffer)
c) you need IP's as integers...fortunately for you, I've written code that does that.
public static int StringIPToInt(string IPString)
{
string[] _splitString = IPString.Split(new char[]{'.'});
int _retVal = 0;
if (_splitString.Length < 4)
throw new ArgumentException("Please pass in a valid IP in dotted-quad notation!","IPString");
else
{
_retVal += (int)(int.Parse(_splitString[3]) * Math.Pow(256,0));
_retVal += (int)(int.Parse(_splitString[2]) * Math.Pow(256,1));
_retVal += (int)(int.Parse(_splitString[1]) * Math.Pow(256,2));
_retVal += (int)(int.Parse(_splitString[0]) * Math.Pow(256,3));
}
_retVal = (int) (((_retVal & 0x000000ff) << 24) +
((_retVal & 0x0000ff00) << 8) +
((_retVal & 0x00ff0000) >> 8) +
((_retVal & 0xff000000) >> 24));
return _retVal;
}
I won't go into detail except that all the compound addition you see is parsing the dotted-quad notation and creating an integer value to represent it (dotted-quad just stores the 32 bits of an IP address as four octets, so it's easy to go back to a 32bit integer). The last bit of code with all the bitshifting and binary AND's flips the endianness of the output integer so that you have the correct value.
So, a call like this may be appropriate:
byte[] outputData;
int dataLength = 0;
SendARP(StringIPToLong("192.168.1.101"),StringIPToLong("127.0.0.1"),outputData,ref dataLength);
I would then use BitConverter.ToString() or something similar to get human-readable data out of that buffer byte[].
|
|
|
|
|
Thanks for the fast reply.
I think i have figured the SendARP() out now, but i still can't give it the IP destination in integer.
i tried with the code you provieded me, like this:
<br />
public partial class _Default : System.Web.UI.Page <br />
{<br />
[DllImport("iphlpapi.dll", ExactSpelling = true)]<br />
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);<br />
<br />
protected void Page_Load(object sender, EventArgs e)<br />
{<br />
<br />
}<br />
protected void Button1_Click(object sender, EventArgs e)<br />
{<br />
byte[] outputData = new byte[6];<br />
int dataLength = outputData.Length;<br />
SendARP(StringIPToLong("192.168.1.101"), StringIPToLong("127.0.0.1"), outputData, ref dataLength);<br />
Label1.Text = "MAC:" + BitConverter.ToString(outputData, 0, 6);<br />
}<br />
<br />
public static int StringIPToInt(string IPString)<br />
{<br />
string[] _splitString = IPString.Split(new char[] { '.' });<br />
int _retVal = 0;<br />
if (_splitString.Length < 4)<br />
throw new ArgumentException("Please pass in a valid IP in dotted-quad notation!", "IPString");<br />
else<br />
{<br />
_retVal += (int)(int.Parse(_splitString[3]) * Math.Pow(256, 0));<br />
_retVal += (int)(int.Parse(_splitString[2]) * Math.Pow(256, 1));<br />
_retVal += (int)(int.Parse(_splitString[1]) * Math.Pow(256, 2));<br />
_retVal += (int)(int.Parse(_splitString[0]) * Math.Pow(256, 3));<br />
}<br />
_retVal = (int)(((_retVal & 0x000000ff) << 24) +<br />
((_retVal & 0x0000ff00) << 8) +<br />
((_retVal & 0x00ff0000) >> 8) +<br />
((_retVal & 0xff000000) >> 24));<br />
return _retVal;<br />
}<br />
The error i get is:
Default.aspx.cs(24): error CS0103: The name
'StringIPToLong' does not exist in the current context
That sound like it can't find the StingIPToLong function in the current codeblock, but the function is declared "public" so i thought that it worked in all codeblocks.
|
|
|
|
|
GentooBoxX wrote:
That sound like it can't find the StingIPToLong function in the current codeblock, but the function is declared "public"
Look at your code. You renamed the function to "StingIPToInt" but are calling "StringIPToLong"
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
Thanks
Typical newbie error.
|
|
|
|
|
I actually renamed the function when I was typing the comment (so as to simplify for you - I represented an IP as a long variable instead of an int in my app).
Did it help you with what you needed?
|
|
|
|
|
|
i have a form on which i have drawn some text using drawString(...) method. now i want to zoon that text. how can i do that??????
|
|
|
|
|
Zooming? Wouldn't it be better if you specified a different font size when you draw it?
---
b { font-weight: normal; }
|
|
|
|
|
Hello everybody.
I want to transfer my software to another computer which doesn't install the visual studio.net. I tried to copy the .exe file to the target machine, obviously it doesn't work. Is there any installer or setup package to achieve this in C#???
Could anybody help me with this please??
Thanks in advance
Asura
|
|
|
|
|
Your customers need to have the .NET Runtime 1.1 installed.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Is the runtime 1.1 in the installation disk of Visual .net???
Can I just copy the libraries I need to the target computer???
Thanks
Asura
|
|
|
|
|
This is what your clients need.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
i have written an asynchronous server. when one client connects to the server there is no problem. the problem appears when more than one client tries to connect to the server on the same computer. the exception seems to be raised when i bind the server to a certain ip and then start listening to a spesific port:
obj_Socket.Bind(ListenEP);
obj_Socket.Listen(11000);
|
|
|
|
|
|
A port can be bound only once, you're probably trying to bind it more than once (every time a client connects?) .
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
hi,
i have a datagrid on a usercontrol which has two columns of DateTimePicker style. They are 'From Date' and 'To Date'.
when i enter data in the first row, then i also choose the date. So when i add data in the second row, the dateTimePicker in 'From Date ' Should show me the next day of the date that i entered in the 'To Date' of the previous column. This should happen for all the rows that i enter..
could ne1 plz tell me how to do this??
Any help is appreciated,
Thanks
|
|
|
|
|
Hi,
I'm having some problems with DLLImport'ing into an application.
I have a .dll which has no com interface and requires importing of each function I wish to use.
These DLLImport lines sit in a class name VeryWrap
It's a PDF toolkit, and I'm able to create a pdf fine without any content with:
[DllImport("verywrite.dll", EntryPoint="VeryCreate", ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]<br />
public static extern long pVeryCreate(char[] filename);
Then I run this with:
long id = veryWrap.VeryCreate("Test1.pdf");
I get a file names Test1.pdf, great! I know the technique works.
But what I need is to insert an image into this file.
The library provides a function that takes an image filename and returns the width and height of the image which I then use to create the correct pdf page size I need.
But the return value of the function is a long stating success or failure of the function call.
The width and height are by as paramaters by reference to the function. These values aren't being written to.
The filename as defined in the original C function prototypes are char *, so I thought I should do 'char[] filename'. It seems to work for the Create function.
Now Here's my declaration for the Get Function:
[DllImport("verywrite.dll",EntryPoint="VeryGetFunction",ExactSpelling=false,CallingConvention=CallingConvention.Cdecl)]<br />
public static extern long pVeryGetFunction_ByRef(long id,long func_code,ref long para1,ref long para2,char[] para3,char[] para4);
para1 and para2 are the parameters in which I use my width and height variables, and para3 is the filename.
My call to this function is:
ret = pVeryGetFunction_ByRef(id, func_code, ref width, ref height, filename.ToCharArray(), filename.ToCharArray());
The value of width and height do not change but they should be the width and height of the image (filename).
Does anyone have any idea if I'm missing something?
Thanks,
Steve
|
|
|
|
|
Hello. I have written a fully functional program and I was going to let my sister try it out and give me feedback, however when I send her the .exe file generated by Visual Studio .NET 2003, she gets an error: "An exception 'System.IO.FileNotFoundException' has occured in .exe. If I copy all of my references to the output directory and send her the directory, then she doesn't get this error. Is there any way at all to wrap the references into the .exe? I thought about using the DllImport that C# provides, but it seemed to only work on attributes, where I obvioudly need everything. How would I solve this problem?
Mike - I love to program!
|
|
|
|
|
You do need the referenced dlls to be present in the application directory. Why do you need to have a single exe? If the problem is sending multiple files, can't you just zip the output directory and send it?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
It is more pleasing to have everything inside one dll without any files that can be modified by users which will screw up the program. If I do not need the referenced dlls, how come when I put them in a single folder with the .exe it works, however when I dont it does not. Am I missing something?
Mike - I love to program!
|
|
|
|
|
Of course you need the referenced assemblies, why would you reference them if you didn't need them?
It's quite simple: Your application is requesting a Type and the framework has to find the definition of this Type somewhere.
The places .NET looks for is the application's directory and the GAC.
If the assembly holding the requested Type cannot be found you'll get a FileNotFound error.
DllImport is only used to access functions in legacy DLLs, not Types in .NET assemblies.
Regards,
mav
|
|
|
|
|
Thanks for the information mav. Does that mean that I won't be able to wrap the .dlls with my .exe?
I love to program!
|
|
|
|
|
Well, you _could_, but I don't really see the benefit.
You'd have to build the dlls your using, add them to your main program as embedded resources, extract them at runtime and then load them manually (for example using Assembly.LoadFrom()).
Really a waste of time IMHO.
mav
|
|
|
|
|
I see. I wouldn't even know how to begin doing that (I would try it though if you could provide a tutorial on how to do so), and ive accepted the fact that I'll have to keep the .dlls out.
Mike - I love to program!
|
|
|
|