|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) I just had it in my archive maybe simplify your work but it in VB
<br />
<br />
Option Explicit<br />
<br />
'// define constants<br />
Private Const IP_SUCCESS As Long = 0<br />
Private Const SOCKET_ERROR As Long = -1<br />
<br />
Private Const MAX_WSADescription As Long = 256<br />
Private Const MAX_WSASYSStatus As Long = 128<br />
Private Const MIN_SOCKETS_REQD As Long = 1<br />
<br />
Private Const WS_VERSION_REQD As Long = &H101<br />
'Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD / &H100 And &HFF&<br />
Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&<br />
<br />
Private Const WSADescription_Len As Long = 256<br />
Private Const WSASYS_Status_Len As Long = 128<br />
Private Const AF_INET As Long = 2<br />
<br />
'// structures<br />
<br />
Private Type HOSTENT<br />
hName As Long<br />
hAliases As Long<br />
hAddrType As Integer<br />
hLength As Integer<br />
hAddrList As Long<br />
End Type<br />
<br />
<br />
Private Type WSADATA<br />
wVersion As Integer<br />
wHighVersion As Integer<br />
szDescription(0 To MAX_WSADescription) As Byte<br />
szSystemStatus(0 To MAX_WSASYSStatus) As Byte<br />
wMaxSockets As Long<br />
wMaxUDPDG As Long<br />
dwVendorInfo As Long<br />
End Type<br />
<br />
'
'kernel32<br />
Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nBytes As Long)<br />
Private Declare Function apiStrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long<br />
'wsock32<br />
Private Declare Function apiGetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal hostname As String) As Long<br />
Private Declare Function apiWSAStartup Lib "wsock32.dll" Alias "WSAStartup" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long<br />
Private Declare Function apiWSACleanup Lib "wsock32.dll" Alias "WSACleanup" () As Long<br />
Private Declare Function apiInetAddr Lib "wsock32.dll" Alias "inet_addr" (ByVal s As String) As Long<br />
Private Declare Function apiGetHostByAddr Lib "wsock32.dll" Alias "gethostbyaddr" (haddr As Long, ByVal hnlen As Long, ByVal addrtype As Long) As Long<br />
<br />
'// private functions<br />
Private Function InitializeSocket() As Boolean<br />
Dim WSAD As WSADATA<br />
<br />
'attempt to initialize the socket<br />
InitializeSocket = apiWSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS<br />
End Function<br />
<br />
Private Sub CloseSocket()<br />
'try to close the socket<br />
If apiWSACleanup() <> 0 Then<br />
MsgBox "Error calling apiWSACleanup.", vbCritical<br />
End If<br />
<br />
End Sub<br />
<br />
Public Function GetIPFromHostName(ByVal sHostName As String) As String<br />
'converts a host name to an IP address.<br />
<br />
Dim nBytes As Long<br />
Dim ptrHosent As Long<br />
Dim hstHost As HOSTENT<br />
Dim ptrName As Long<br />
Dim ptrAddress As Long<br />
Dim ptrIPAddress As Long<br />
Dim sAddress As String 'declare this as Dim sAddress(1) As String if you want 2 ip addresses returned<br />
<br />
'try to initalize the socket<br />
If InitializeSocket() = True Then<br />
<br />
'try to get the IP<br />
ptrHosent = apiGetHostByName(sHostName & vbNullChar)<br />
<br />
If ptrHosent <> 0 Then<br />
<br />
'get the IP address<br />
apiCopyMemory hstHost, ByVal ptrHosent, LenB(hstHost)<br />
apiCopyMemory ptrIPAddress, ByVal hstHost.hAddrList, 4<br />
<br />
'fill buffer<br />
sAddress = Space$(4)<br />
'if you want multiple domains returned,<br />
'fill all items in sAddress array with 4 spaces<br />
<br />
apiCopyMemory ByVal sAddress, ByVal ptrIPAddress, hstHost.hLength<br />
<br />
'change this to<br />
'CopyMemory ByVal sAddress(0), ByVal ptrIPAddress, hstHost.hLength<br />
'if you want an array of ip addresses returned<br />
'(some domains have more than one ip address associated with it)<br />
<br />
'get the IP address<br />
GetIPFromHostName = IPToText(sAddress)<br />
'if you are using multiple addresses, you need IPToText(sAddress(0)) & "," & IPToText(sAddress(1))<br />
'etc<br />
End If<br />
Else<br />
MsgBox "Failed to open Socket."<br />
End If<br />
End Function<br />
<br />
Private Function IPToText(ByVal IPAddress As String) As String<br />
'converts characters to numbers<br />
IPToText = CStr(Asc(IPAddress)) & "." & _<br />
CStr(Asc(Mid$(IPAddress, 2, 1))) & "." & _<br />
CStr(Asc(Mid$(IPAddress, 3, 1))) & "." & _<br />
CStr(Asc(Mid$(IPAddress, 4, 1)))<br />
End Function<br />
<br />
Public Function GetHostNameFromIP(ByVal sIPAddress As String) As String<br />
<br />
Dim ptrHosent As Long<br />
Dim hAddress As Long<br />
Dim sHost As String<br />
Dim nBytes As Long<br />
<br />
'try to open the socket<br />
If InitializeSocket() = True Then<br />
<br />
'convert string address to long datatype<br />
hAddress = apiInetAddr(sIPAddress)<br />
<br />
'check if an error ocucred<br />
If hAddress <> SOCKET_ERROR Then<br />
<br />
'obtain a pointer to the HOSTENT structure<br />
'that contains the name and address<br />
'corresponding to the given network address.<br />
ptrHosent = apiGetHostByAddr(hAddress, 4, AF_INET)<br />
<br />
If ptrHosent <> 0 Then<br />
<br />
'convert address and<br />
'get resolved hostname<br />
<br />
apiCopyMemory ptrHosent, ByVal ptrHosent, 4<br />
<br />
nBytes = apiStrLen(ByVal ptrHosent)<br />
<br />
If nBytes > 0 Then<br />
'fill the IP address buffer<br />
sHost = Space$(nBytes)<br />
<br />
apiCopyMemory ByVal sHost, ByVal ptrHosent, nBytes<br />
GetHostNameFromIP = sHost<br />
End If<br />
Else<br />
MsgBox "Call to gethostbyaddr failed."<br />
End If<br />
'close the socket<br />
CloseSocket<br />
Else<br />
MsgBox "Invalid IP address"<br />
End If<br />
Else<br />
MsgBox "Failed to open Socket"<br />
End If<br />
End Function<br />
<br />
<br />
|
|
|
|
|
Thanks
Just scanned it but the 'GetHostNameFromIP' looks promising.
Luckily vb is not that difficult to convert to C#.
Hope you have a great day
WvdW
|
|
|
|
|
Hello,
I posted this on msdn thread too but haven't gotten any answer yet so was wondering if you guys can help.
===================
Ahh question about accessing Windows form from a thread created through thread.
When I tried to make sure it's thread-safe, it keeps looping back into InvokeRequired because for some reason the calling thread and creating thread are never the same... Here's a watered down example...
private void blahblahblah()<br />
{<br />
<br />
Thread t = new Thread(method);<br />
t.Start(); <br />
<br />
}<br />
<br />
private void method()<br />
{<br />
<br />
if(InvokeRequired)<br />
{<br />
<br />
<br />
}<br />
<br />
else<br />
{<br />
<br />
for(int i = 0; i < Number; i++)<br />
{<br />
<br />
<br />
Timer timer = new Timer();<br />
timer.Elapsed += new ElapsedEventHandler(andAnother);<br />
_Event.WaitOne();<br />
<br />
}<br />
<br />
}<br />
<br />
}<br />
<br />
<br />
<br />
private void andAnother()<br />
{<br />
<br />
if(InvokeRequired)<br />
{<br />
<br />
<br />
EventHandler Handler = new EventHandler(andAnother);<br />
this.Invoke(Handler, sender, e);<br />
<br />
}<br />
<br />
else<br />
{<br />
<br />
<br />
_Event.Set();<br />
<br />
}<br />
<br />
}
So on both threads, I want to access Windows Form. The first thread is good but the second thread that was created through the first thread loops through InvokeRequired.
Thanks!
|
|
|
|
|
Two pieces of information are missing:
- in what class is all this code, i.e. whose InvokeRequired are you using ?
- and what thread created the instance of that class having the problem ?
The comments don't match with the code:
- "Do something to Windows Form" and what you do is create a Timer (without sufficient
initialization I would say)
- "Doing something to Windows Form" and what you do is set an event.
Furthermore, the GUI thread should not be kept waiting on something.
ictoane wrote: it keeps looping back
what exactly does that mean ? are you getting a StackOverflowException ?
please provide exact code, with proper comment, and the symptoms that go with it.
The better your question is, the sooner you will get a good answer.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hello,
The comments "Do something to Windows Form" are replacements for codes dealing with Windows Forms to eliminate unncessary blocks of code that are irrelevant to the question. I just left them out because they're basically doing something to Windows Form like setting up the ProgressBar or changing the Label.
Basically I want to have a method that creates two threads. And for each of those two threads, create System.Timers.Timer to call on an event to update the ProgressBar and other Windows Forms every second. The two threads need to start on the same time but performs different tasks so I can't skip the threads and go immediately to Timers.
I want to interact with Windows Form on the first layer thread as well as the second layer thread. The way to call Controls in a thread safe manner is to use if(this.InvokeRequired) (<a href="http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx">)</a>[<a href="http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx" target="_blank" title="New Window">^</a>). However, this doesn't work when I use it on the second layer thread.
Therefore, the problem I'm getting is that whenever I use this.InvokeRequired on the second layer thread created by the first layer thread, "it keeps looping back". I'm not intentionally making it to loop back.
Thanks
|
|
|
|
|
Your biggest problem with this code is that the _Event.WaitOne(); is causing the GUI thread to wait until signalled.
This is a great example of a deadlock situation.
Phil
|
|
|
|
|
Not exactly. _Event.WaitOne() is actually called on another thread besides GUI thread so it's not telling the GUI thread to wait but the other thread to wait. Also, the problem occurs before it gets to _Event.WaitOne(). It happens when the Timer starts. Inside the method called by the Timer it keeps invoking itself.
if (this.InvokeRequired)
{
EventHandler Handler = new EventHandler(startProgressBar);
this.BeginInvoke(Handler, sender, e);
}
else
{
...
progressBar1.PerformStep();
...
}
|
|
|
|
|
You know best, we can not possibly improve what you do.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I'm trying to get help on the forum here not looking for sarcastic comments. You're very rude...
|
|
|
|
|
You are new on this forum. Maybe you dont understand how it works.
If you choose to ignore both valid remarks and questions asked, you will be on your own.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
How exactly am I ignoring valid remarks? I got two reponses and I replied both of them the best I could. For your response, you commented about "Do this on Windows Form" which I explained what it really meant etc. For the other response, the person pointed out a possible Deadlock which I replied that it's not the problem because in the code it doesn't even get to that part before it freezes up. All I'm doing is trying to point out the problem I'm having and clearifying it. Both reponses I received did not solve the problem so am I suppose to say thanks and not pursue it any more further simply because I'm new and "don't understand how it works?" Thanks but no thanks. I'll go somewhere else. Thanks for your help. Have a good day.
|
|
|
|
|
For us on this forum to be able to drill down to the actual problem being experienced with this code you will have to post a more explicit example. Otherwise, we have to make assumptions and our assumptions will be incorrect.
Phil
|
|
|
|
|
Hello All
I m facing one problem in C# Windows Application
That one is RPC Server Unavailable while open excel sheet
I m using excel object com object
i just want to import data into SQL Database
first i open the excel sheet and then read the excel finally store data into sql
but Excel object throw one error RPC Server unavailable
The error occur in the following code
WorkBook = ExpApp.Workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\Clone.xls", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "\t", true, false, 0, true);
anybody plz help me
it s very urgent
Thanks in Advance
Sak
|
|
|
|
|
|
Thanks for ur command
but i m using Excel COM Object for reading excel data
so i need solution for rpc server unavailable using Excel dll
Thanks
Sak
|
|
|
|
|
How do I declare a queue of string arrays?
Queue<string[]> qVar = new Queue<???>;
|
|
|
|
|
you almost did that just here is the complete one
Queue<string[]> qString = new Queue<string[]>();
good luck
|
|
|
|
|
Ahh, I was trying Queue<string[]> but forgot the () after the fact.
Thanks!
|
|
|
|
|
Hi,
DataGridView gives only primary sort (by one column).
How can i define a secondary sort in a bound data GridView?
(I saw a solution for unbound data only)
Thanks
dana Tov
|
|
|
|
|
I have the following problem
-I created a form with an tab control
-I the tab control contains (minimum) 2 tab pages
-In the second (non selected) tab page i have a control
-And there is another formular (irrelevant content)
<br />
#region PREPERATION<br />
Form f = new Form();<br />
TabControl t = new TabControl();<br />
<br />
TabPage tp1 = new TabPage("page1");<br />
TabPage tp2 = new TabPage("page2");<br />
<br />
Button b = new Button();<br />
b.Size = new Size(100, 20);<br />
<br />
tp2.Controls.Add(b);<br />
<br />
t.TabPages.Add(tp1);<br />
t.TabPages.Add(tp2);<br />
<br />
this.Controls.Add(t);<br />
t.BringToFront();<br />
#endregion PREPERATION<br />
When i first open the child-form with Show() and then select the tab page with the control the parent-form is focused again.
f.Show();<br />
t.SelectedIndex = 1;
How can i force the child-form to keep its focus, while the parent change the tab page?
I don't want to use ShowDialog() or Show(this).
Is this a bug or a feature of the tab control (tab page)?
(Is the only way to fix it, changing the order of the source code?)
|
|
|
|
|
Try tp2.Select(); You don't want to select the tab control, but the actual tab with the control on it.
Hogan
|
|
|
|
|
It does not work.
It don't show the tab page (tp2).
|
|
|
|
|
Does anyone have another idea?
|
|
|
|
|
Hi
Can anyone tell me how to generate a date in random in the interval of two dates (Max date and the Min date)...
Please anyone suggest the answer soon as my due date is nearing
Thx
Anjum
|
|
|
|
|
search on google for "c# random" it'll tell you about the random class ... the random class takes a min int and a max int ... the rest you just need to code.
|
|
|
|