|
Sorry, but I do not understand what you mean. If you have a common dll then you only need it installed once.
|
|
|
|
|
This is possibly a misunderstanding because the word you are using mean something else in this context: perhaps you should try explaining again in more detail exactly what is giving you a problem?
Examples help as well!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
cant send file bigger that 1 megabit ( Server_client )
private byte[] GetFileInfo(string fname, string fext, long fsize)
{
byte[] data = new byte[1024];
byte[] type = Encoding.UTF8.GetBytes("fff");
byte[] ext = Encoding.UTF8.GetBytes(fext);
byte[] extE = Encoding.UTF8.GetBytes(":");
byte[] sizeS = Encoding.UTF8.GetBytes(fsize.ToString());
byte[] sizeE = Encoding.UTF8.GetBytes("s");
byte[] name = Encoding.UTF8.GetBytes(fname);
byte[] nameE = Encoding.UTF8.GetBytes("?");
Array.Copy(type, data, type.Length);
Array.Copy(ext, 0, data, type.Length, ext.Length);
Array.Copy(extE, 0, data, ext.Length + type.Length, extE.Length);
Array.Copy(sizeS, 0, data, ext.Length + type.Length + extE.Length, sizeS.Length);
Array.Copy(sizeE, 0, data, ext.Length + type.Length + extE.Length + sizeS.Length, sizeE.Length);
Array.Copy(name, 0, data, ext.Length + type.Length + extE.Length + sizeS.Length + sizeE.Length, name.Length);
Array.Copy(nameE, 0, data, ext.Length + type.Length + extE.Length + sizeS.Length + sizeE.Length + name.Length, nameE.Length);
byte[] newdata = new byte[ext.Length + extE.Length + sizeS.Length + sizeE.Length + name.Length + nameE.Length + type.Length];
Array.Copy(data, newdata, newdata.Length);
return newdata;
}
private byte[] GetBinaryFile(string filename)
{
byte[] bytes;
using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
}
return bytes;
}
}
}
modified 7-Jun-18 1:26am.
|
|
|
|
|
Please delete your duplicate of this question. Also, edit this and remove all the code not relating to the problem, and add some details about exactly what happens.
|
|
|
|
|
Is this for a DOS attack?
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Is that MS-DOS or PC-DOS?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
|
You programmed all that, and can't figure out what determines the length of the buffer?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
When the 102400000 byte buffer (10megabit buffer length) ,software receives an error
|
|
|
|
|
Hussein Tb wrote: the 102400000 byte buffer (10megabit buffer length)
When I last did arithmetic, that number is over 100 million, not ten. And it's bytes, not bits.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
hi gang i have a litle problem c# ...
i have 4 textbox i need to calculate first (tb2 x tb3 x 60) after tb1 / (the result) and after x 100
and show the result in texbox4
textbox4 = textbox1 / (textbox2 x textbox3 x 60) x 100
|
|
|
|
|
First, use int.TryParse (or double.TryParse if appropriate) to convert each value to an numeric:
double t1;
if (!double.TryParse(textBox1.Text, out t1))
{
... Report problem to user - he typed a bad number ...
return;
} When you have the four values, it's trivial:
TextBox4.Text = (t1 / (t2 * t3 * 60 ) * 100).ToString();
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
What happened to "We do not do your homework?"
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It's a fine line between "doing the homework" and "giving enough so they can finish it", isn't it?
I didn't think just pointing him at the TryParse documentation would be enough, he is a beginner after all - and thoroughly lost by the look of it!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
On second thought, I think you're right. Your mentality is not "throw 'em to the wolves", but do what you can to help.
There is a difference between asking for a clue and asking for it to be done for you. Your post embodies the teaching spirit.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thank you.
I think that's the nicest thing anyone has said to me all week.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am writing a C# application with Visual Studio Express 2005 and I need to query the available com ports and their friendly names. The first part is easy, I can simply call SerialPort.GetPortNames(). In order to get the friendly names, I found the following code on the internet:
for (int i = 0; i < availableComPorts.Length; i++)
{
try
{
string temp = "(" + availableComPorts[i] + ")";
foreach (ManagementObject queryObj in searcher.Get())
{
if ((queryObj["Name"] != null) && (queryObj["Name"].ToString().EndsWith(temp)))
{
ComPortInfo comPortsInfo = new ComPortInfo();
comPortsInfo.officalName = availableComPorts[i];
comPortsInfo.userfriendlyDescription = queryObj["Caption"].ToString().Replace("(" + availableComPorts[i] + ")", "").Trim();
comPortsInfo.manufacturer = queryObj["Manufacturer"].ToString();
comPortInfoArrayList.Add(comPortsInfo);
}
}
}
catch (Exception)
{
}
}
The problem with the code above is that it takes about 1.5 seconds(!) to execute on my computer which is too slow. After further investigations I found the following post [winapi - How do I get a list of available serial ports in Win32? - Stack Overflow](https://stackoverflow.com/questions/1388871/how-do-i-get-a-list-of-available-serial-ports-in-win32) which refers to the following project: [CEnumerateSerial v1.34](http://www.naughter.com/enumser.html). This project seems very promising, especially the "Device Manager (SetupAPI - GUID_DEVINTERFACE_COMPORT)" method, which executes extremely quickly on my computer:
_Return_type_success_(return != 0) BOOL CEnumerateSerial::QueryRegistryPortName(_In_ ATL::CRegKey& deviceKey, _Out_ int& nPort)
{
BOOL bAdded = FALSE;
LPTSTR pszPortName = nullptr;
if (RegQueryValueString(deviceKey, _T("PortName"), pszPortName))
{
const size_t nLen = _tcslen(pszPortName);
if (nLen > 3)
{
if ((_tcsnicmp(pszPortName, _T("COM"), 3) == 0) && IsNumeric((pszPortName + 3), FALSE))
{
nPort = _ttoi(pszPortName + 3);
bAdded = TRUE;
}
}
LocalFree(pszPortName);
}
return bAdded;
}
_Return_type_success_(return != 0) BOOL CEnumerateSerial::QueryUsingSetupAPI(const GUID& guid, _In_ DWORD dwFlags, _Inout_ CPortsArray& ports, _Inout_ CNamesArray& friendlyNames)
{
#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
ports.clear();
friendlyNames.clear();
#else
ports.RemoveAll();
friendlyNames.RemoveAll();
#endif //#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
HDEVINFO hDevInfoSet = SetupDiGetClassDevs(&guid, nullptr, nullptr, dwFlags);
if (hDevInfoSet == INVALID_HANDLE_VALUE)
return FALSE;
BOOL bMoreItems = TRUE;
int nIndex = 0;
SP_DEVINFO_DATA devInfo = { 0 };
while (bMoreItems)
{
devInfo.cbSize = sizeof(SP_DEVINFO_DATA);
bMoreItems = SetupDiEnumDeviceInfo(hDevInfoSet, nIndex, &devInfo);
if (bMoreItems)
{
BOOL bAdded = FALSE;
ATL::CRegKey deviceKey;
deviceKey.Attach(SetupDiOpenDevRegKey(hDevInfoSet, &devInfo, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE));
if (deviceKey != INVALID_HANDLE_VALUE)
{
int nPort = 0;
if (QueryRegistryPortName(deviceKey, nPort))
{
#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
ports.push_back(nPort);
#else
ports.Add(nPort);
#endif //#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
bAdded = TRUE;
}
}
if (bAdded)
{
ATL::CHeapPtr<BYTE> byFriendlyName;
if (QueryDeviceDescription(hDevInfoSet, devInfo, byFriendlyName))
{
#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
friendlyNames.push_back(reinterpret_cast<LPCTSTR>(byFriendlyName.m_pData));
#else
friendlyNames.Add(reinterpret_cast<LPCTSTR>(byFriendlyName.m_pData));
#endif //#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
}
else
{
#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
friendlyNames.push_back(_T(""));
#else
friendlyNames.Add(_T(""));
#endif //#ifndef CENUMERATESERIAL_MFC_EXTENSIONS
}
}
}
++nIndex;
}
Now, the problem is that I don't know how to convert this to C#. I was able to convert some of it into C# using P/Invoke, for example:
[DllImport("setupapi.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
[MarshalAs(UnmanagedType.LPTStr)] string Enumerator,
IntPtr hwndParent,
uint Flags);
[StructLayout(LayoutKind.Sequential)]
struct SP_DEVINFO_DATA
{
public UInt32 cbSize;
public Guid ClassGuid;
public UInt32 DevInst;
public IntPtr Reserved;
}
[DllImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet,
uint MemberIndex,
ref SP_DEVINFO_DATA DeviceInfoData);
but I'm unable to figure out how to convert the calls to ATL. Can anybody please advice on how I can convert the code to C#?
|
|
|
|
|
You should first figure out why your first solution was "slow" before jumping to a "new" solution.
And "slow" is relative.
Perhaps you're not accounting for dll load time; or should be doing your "port query" at startup as a "parallel" task.
And what do you mean by "friendly names"?
The port names are "COMx"; what do you expect to see?
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I usually link in my dll into the .exe file to speed up the loading, but in this case I would prefer to not have to use a dll, instead I want to translate the C++ code into c# but I run into trouble with the ATL stuff. I need to enumerate com ports several times during runtime, it's not enough to do it only once.
Slow is more than 50 ms, less than that is fast. Here are some measurements from a StackOverflow forum thread that I apparently am not allowed to link to:
"SetupAPI1" using calls to SETUPAPI.DLL
Found com0com ports, also reported "friendly names", took 15ms.
"SetupAPI2" using calls to SETUPAPI.DLL
Did not find com0com ports, reported "friendly names", took 32ms.
Here are examples of friendly names:
Com1 "USB Serial Port"
Com2 "USB Serial Device"
Com3 "STMicroelectronics STLink Virtual COM Port"
|
|
|
|
|
Users perceive "lag" above 100ms.
My experience with users and com ports is that you burden them at (app) "install" time, when they also need to refer to the Control Panel Settings, you save their settings, and then let them forget about "COM" ports.
Any "refreshes" are done on demand. It's not repetetive.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
It's for a control software for a product with an ARM-processor with an embedded USB-controller that resets (and thus drops the USB-connection) when the ARM-processor is reset and when it jumps from application mode to bootloader mode and vice versa.
I'm not so fond of the idea to turn the C++ code into a dll and then call it from C# because my C++ skills are non-existent. Also, I foresee problems passing string arrays (I don't know the needed array size in the C# code) and avoiding memory leaks due to not handling de-allocation of the string arrays in the C++ code properly.
modified 5-Jun-18 16:39pm.
|
|
|
|
|
My reference to dll's was to "system / .NET" dll's that require "load time" when first called.
In my case, I need to maintain connections to "IOT" devices (real time control and data collection).
I have a separate thread that checks the last status of each connection, and "reconnects" if that's what's called for (With a sample rate of 10 seconds, I check each connection every 10 seconds).
(I use C# dlls extensively to abstract API's of all sorts; passing data in different "representations" has never been as issue. XML or Binary Writer / Reader are your friends.)
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
The first part of the C++ code is enumerating the port names like SerialPort.GetPortNames() does. So there is no need to convert that. It is also not recommended to use the registry directly because it may change with the used Windows version while SerialPort.GetPortNames() is guaranteed to provide the correct information.
I don't know why your WMI query is so slow. Maybe you have used an improper search query (should be "SELECT * FROM Win32_SerialPort").
If you want to use the SetupApi with C# see Enumerate All COM Ports and Find Their Name and Description in C#.
Note also that you only need to update the list when a port has been added or removed. This can be handled by registering for device change events.
|
|
|
|
|
Thank you, that link made my life a lot easier.
|
|
|
|
|