I'm having a little problem with my application while trying to use WindowsAPI...
I'm trying to connect to a handle in a way it works, but if I change the code it doesn't works anymore...
The code that works:
handle_t porta; // Global var
COMMTIMEOUTS tempos; // Global var
DCB configuracao; // Global var
wchar_t pcCommPort[]= TEXT("COM1");
//gate address to be accessed (COM1)
The code I'm trying to do:
handle_t porta; // Global var
COMMTIMEOUTS tempos; // Global var
DCB configuracao; // Global var
String GATE = "COM" + Label1->Text;
wchar_t pcCommPort[]= TEXT(GATE);
//gate address to be accessed (Any gate)
I've also tried it:
handle_t porta; // Global var
COMMTIMEOUTS tempos; // Global var
DCB configuracao; // Global var
wchar_t pcCommPort[]= TEXT("COM" + Label1->Text);
//gate address to be accessed (Any gate)
And it:
handle_t porta; // Global var
COMMTIMEOUTS tempos; // Global var
DCB configuracao; // Global var
String GATE = "COM" + Label1->Text;
wchar_t pcCommPort[]= GATE;
//gate address to be accessed (Any gate)
In any way or it says that I haven't mede the correct syntax or the error: Cannot convert unicode string into a wchar_t.
EDIT:
Full source (function):
void abrirporta(){
wchar_t pcCommPort[]= TEXT("COM1");
//endereço da porta a ser acessada (COM1)
//porta foi declarado como HANDLE na seção private da declaração de classe Form
//HANDLE porta
porta = CreateFile(pcCommPort,GENERIC_READ+GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(porta == INVALID_HANDLE_VALUE){
log(7);
throw Exception("Não foi possível abrir a porta COM.\nPor favor, tente novamente!");
}
//Tempo máximo entre o recebimento de 2 bytes (ms)
tempos.ReadIntervalTimeout = 20;
//Multiplicador de tempo de recebimento por byte
tempos.ReadTotalTimeoutMultiplier = 0;
tempos.ReadTotalTimeoutConstant = 100;
tempos.WriteTotalTimeoutMultiplier = 0;
tempos.WriteTotalTimeoutConstant = 100;
if(!SetCommTimeouts(porta ,&tempos))
{
CloseHandle(porta);
frmPrincipal->spLig->Visible = False;
frmPrincipal->spStatusInd->Visible = False;
log(6);
throw Exception("Erro na configuração de Timeout");
}
GetCommState(porta, &configuracao);
configuracao.BaudRate = 19200;
configuracao.ByteSize = 8;
configuracao.Parity = NOPARITY;
configuracao.StopBits = ONESTOPBIT;
if(!SetCommState(porta,&configuracao))
{
CloseHandle(porta);
frmPrincipal->spLig->Visible = False;
frmPrincipal->spStatusInd->Visible = False;
log(5);
throw Exception("Erro na Configuração da porta");
}
frmPrincipal->spLig->Visible = True;
frmPrincipal->spStatusInd->Visible = False;
log(3);
frmPrincipal->btEnviar->Enabled = true;
frmPrincipal->swSaida1->Enabled = true;
log(8);
}
I hope you can help me...
Since now thanks XD.
You are making this more difficult then it needs to be. System::String is an alias for System::UnicodeString, which holds wchar_t data on Windows. You don't need to copy the UnicodeString data into a wchar_t[] buffer. You can use the UnicodeString::c_str() method when you need to pass a String to a function that expects wchar_t* parameters, eg:
void abrirporta()
{
String pcCommPort = L"COM" + Label1->Text;
porta = CreateFile(pcCommPort.c_str(), ...);
...
}
Related
Just trying to read a CSV, as indicated in this post:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/read-a-txt-file/m-p/3781245
But keep getting that error, why?
var prompt = "Elegir cuadro de materiales"
var filter = "*";
var multi_select = true;
var selectedFile = File.openDialog(["Elegir cuadro de materiales"], filter, multi_select);
alert(selectedFile);
selectedFile.open('r');
var dataToGet = selectedFile.readln();
selectedFile.close();
Results that openDialog with multiSelect set to true, returns an array.
So, this solves the issue:
var prompt = "Elegir cuadro de materiales"
var filter = "*";
var multi_select = true;
var selectedFile = File.openDialog(["Elegir cuadro de materiales"], filter, multi_select);
alert(selectedFile);
selectedFile[0].open('r');
var dataToGet = selectedFile[0].readln();
selectedFile[0].close();
You've solved your own problem, another way to do it is with a new File
selectedFile = File.openDialog("Elegir cuadro de materiales","File:*.TXT", true);
var myFile = new File(selectedFile[0]); //first in array
myFile.open('r');
// read the file here
myFile.close();
I need to report errors from my application on C++Builder via SNMP.
I started implementing SNMP SendTrap using Indy components.
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
UnicodeString myEnterprise = "1.5.5.5.5.5.5.5";
UnicodeString eventType = "1.5.5.5.5.5.5.5.1";
UnicodeString eventDistance = "1.5.5.5.5.5.5.5.2";
TIdSNMP * idSnmp = 0;
TSNMPInfo * infoSnmp = 0;
idSnmp = new TIdSNMP(NULL);
infoSnmp = new TSNMPInfo(idSnmp);
idSnmp->Host = edHost->Text;
idSnmp->Community = "public";
infoSnmp->Host = edHost->Text;
infoSnmp->Community = "public";
infoSnmp->Enterprise = myEnterprise;
infoSnmp->GenTrap = 6; // I've met such values
infoSnmp->SpecTrap = 1; // somewhere in inet
infoSnmp->MIBAdd(eventType,"ftCritical");
infoSnmp->MIBAdd(eventDistance,"2.357");
idSnmp->SendTrap();
delete idSnmp;
}
But when I run application there is no udp activity in my system. When I run something like this
idSnmp->QuickSend(sysDescr, "public", edHost->Text, val);
wireshark shows 192.168.100.21 192.168.100.19 SNMP 82 get-request 1.3.6.1.2.1.1.3.0
but when idSnmp->SendTrap() wireshark sees nothing (filter for wireshark is UDP portrange 161-162 in both cases)
I'll be glad to see some remarks about my code or maybe working example of SendTrap :)
You are not populating TIdSNMP::Trap with any values. That is why TIdSNMP::SendTrap() is not sending anything. There is nothing for it to send.
Try this instead:
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->Trap->Host = edHost->Text;
idSnmp->Trap->Community = _D("public");
idSnmp->Trap->Enterprise = myEnterprise;
idSnmp->Trap->GenTrap = 6; // I've met such values
idSnmp->Trap->SpecTrap = 1; // somewhere in inet
idSnmp->Trap->MIBAdd(eventType, _D("ftCritical"));
idSnmp->Trap->MIBAdd(eventDistance, _D("2.357"));
idSnmp->SendTrap();
delete idSnmp;
}
Alternatively, you can use TIdSNMP::QuickSendTrap() instead:
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
TStringList *names = new TStringList;
names->Add(eventType);
names->Add(eventDistance);
TStringList *values = new TStringList;
values->AddObject(_D("ftCritical"), (TObject*)ASN1_OCTSTR);
values->AddObject(_D("2.357"), (TObject*)ASN1_OCTSTR);
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->QuickSendTrap(edHost->Text, myEnterprise, _D("public"), 162, 6, 1, names, values);
delete idSnmp;
delete names;
delete values;
}
Or, if you are compiling for mobile:
void __fastcall TMainForm::btSendTrapClick(TObject *Sender)
{
String myEnterprise = _D("1.5.5.5.5.5.5.5");
String eventType = myEnterprise + _D(".1");
String eventDistance = myEnterprise + _D(".2");
TIdMIBValueList *mibs = new TIdMIBValueList;
mibs->Add(TIdMIBValue(eventType, _D("ftCritical"), ASN1_OCTSTR));
mibs->Add(TIdMIBValue(eventDistance, _D("2.357"), ASN1_OCTSTR));
TIdSNMP *idSnmp = new TIdSNMP(NULL);
idSnmp->QuickSendTrap(edHost->Text, myEnterprise, _D("public"), 162, 6, 1, mibs);
delete idSnmp;
delete mibs;
}
Right! Here is my attempt at a circular buffer (For use in a graphing program, using the canvas element). Have yet to get round to testing it out.
Question is - Can anyone see any flaws in my logic? Or bottlenecks?
/**
* A circular buffer class.
* #To add value -> bufferObject.addValue(xValue, yValue);
* #To get the First-in value use -> bufferObject.getValue(0);
* #To get the Last-in value use -> bufferObject.getValue(bufferObject.length);
**/
var circularBuffer = function (bufferSize) {
this.bufferSize = bufferSize;
this.buffer = new Array(this.bufferSize); // After testing on jPerf -> 2 x 1D array seems fastest solution.
this.end = 0;
this.start = 0;
// Adds values to array in circular.
this.addValue = function(xValue, yValue) {
this.buffer[this.end] = {x : xValue, y: yValue};
if (this.end != this.bufferSize) this.end++;
else this.end = 0;
if(this.end == this.start) this.start ++;
};
// Returns a value from the buffer
this.getValue = function(index) {
var i = index+this.start;
if(i >= this.bufferSize) i -= this.bufferSize; //Check here.
return this.buffer[i]
};
// Returns the length of the buffer
this.getLength = function() {
if(this.start > this.end || this.start == this.bufferSize) {
return this.xBuffer.length;
} else {
return this.end - this.start;
}
};
// Returns true if the buffer has been initialized.
this.isInitialized = function() {
if(this.end != this.start) return true;
else return false;
};
}
Please feel free to reuse this code.
Updated twice (and tested!).
Update: Found another implementation Circular buffer in JavaScript
Made class variables private, corrected old xBuffer reference. Will do more edits tonight.
/**
* A circular buffer class.
* #To add value -> bufferObject.addValue(xValue, yValue);
* #To get the First-in value use -> bufferObject.getValue(0);
* #To get the Last-in value use -> bufferObject.getValue(bufferObject.length);
**/
var circularBuffer = function (buffer_size) {
var bufferSize = buffer_size;
var buffer = new Array(bufferSize); // After testing on jPerf -> 2 x 1D array seems fastest solution.
var end = 0;
var start = 0;
// Adds values to array in circular.
this.addValue = function(xValue, yValue) {
buffer[end] = {x : xValue, y: yValue};
if (end != bufferSize) end++;
else end = 0;
if(end == start) start++;
};
// Returns a value from the buffer
this.getValue = function(index) {
var i = index+start;
if(i >= bufferSize) i -= bufferSize; //Check here.
return buffer[i];
};
// Returns the length of the buffer
this.getLength = function() {
if(start > end || start == bufferSize) {
return buffer.length;
} else {
return end - start;
}
};
// Returns true if the buffer has been initialized.
this.isInitialized = function() {
return (end != start) ? true : false;
};
}
I implemented Vogomatix's code above, and got a few bugs. The code writes off the end of the buffer, expanding the buffer size automatically, and the addValue function is bound to a particular type. I've adjusted the code to work with any object type, added some private subroutines to simplify, and added a function to dump the contents out to a string, with an optional delimiter. Also used a namespace.
What's missing is a removeValue() but it would be just a check of count to be greater than zero, then a call to _pop().
This was done because I needed a rolling, scrolling text buffer for inbound messages, that did not grow indefinitely. I use the object with a textarea, so I get behaviour like a console window, a scrolling text box that does not chew up memory indefinitely.
This has been tested with expediency in mind, in that I am coding quickly, posted here in the hope that fellow OverFlow-ers use and anneal the code.
///////////////////////////////////////////////////////////////////////////////
// STYLE DECLARATION
// Use double quotes in JavaScript
///////////////////////////////////////////////////////////////////////////////
// Global Namespace for this application
//
var nz = nz || {};
nz.cbuffer = new Object();
///////////////////////////////////////////////////////////////////////////////
// CIRCULAR BUFFER
//
// CREDIT:
// Based on...
// Vogomatix http://stackoverflow.com/questions/20119513/attempt-at-circular-buffer-javascript
// But re-written after finding some undocumented features...
/**
* A circular buffer class, storing any type of Javascript object.
* To add value -> bufferObject.addValue(obj);
* To get the First-in value use -> bufferObject.getValue(0);
* To get the Last-in value use -> bufferObject.getValue(bufferObject.length);
* To dump to string use -> bufferObject.streamToString(sOptionalDelimiter); // Defaults to "\r\n"
**/
nz.cbuffer.circularBuffer = function (buffer_size) {
var bufferSize = buffer_size > 0 ? buffer_size : 1; // At worst, make an array of size 1
var buffer = new Array(bufferSize);
var end = 0; // Index of last element.
var start = 0; // Index of first element.
var count = 0; // Count of elements
// 'Private' function to push object onto buffer.
this._push = function (obj) {
buffer[end] = obj; // Write
end++; // Advance
if (end == bufferSize) {
end = 0; // Wrap if illegal
}
count++;
}
// 'Private' function to pop object from buffer.
this._pop = function () {
var obj = buffer[start];
start++;
if (start == bufferSize) {
start = 0; // Wrap
}
count--;
return obj;
}
// Adds values to buffer.
this.addValue = function (obj) {
if (count < bufferSize) {
// Just push
this._push(obj);
}
else {
// Pop, then push
this._pop();
this._push(obj);
}
}
// Returns a value from the buffer. Index is relative to current notional start.
this.getValue = function (index) {
if (index >= count || index < 0) return; // Catch attempt to access illegal index
var i = index + start;
if (i >= bufferSize) {
i -= bufferSize;
}
return buffer[i];
}
// Returns the length of the buffer.
this.getLength = function () {
return count;
}
// Returns all items as strings, separated by optional delimiter.
this.streamToString = function (delim) {
delim = (typeof delim === "undefined") ? "\r\n" : delim; // Default syntax; Default to CRLF
var strReturn = "";
var once = 0;
var index = 0;
var read = index + start;
for (; index < count; ++index) {
if (once == 1) strReturn += delim.toString();
strReturn += buffer[read].toString();
read++;
if (read >= bufferSize) read = 0;
once = 1;
}
return strReturn;
}
}
I've got a really strange problem. MSVC doesn't have initializer lists, so I've used a lambda to approximate them.
static const std::unordered_map<std::wstring, LexedFile::Token> reserved_words =
[]() -> std::unordered_map<std::wstring, LexedFile::Token> {
std::unordered_map<std::wstring, LexedFile::Token> retval;
// Do stuff with retval
return retval;
}();
MSVC throws a compiler error.
error C2440: 'initializing' : cannot convert from 'int' to
'std::tr1::unordered_map<_Kty,_Ty>'
Unless I'm quite blind, there's no "int" anywhere near this. I don't see what's wrong. Any suggestions?
Edit:
There's nothing funky about // Do stuff with retval, it's just a bunch of insertions, and this is a function-scope static variable in a lambda in a member function.
auto next = [&] {
static const std::unordered_map<std::wstring, LexedFile::Token> reserved_words =
[]() -> std::unordered_map<std::wstring, LexedFile::Token> {
std::unordered_map<std::wstring, LexedFile::Token> retval;
retval[L"namespace"] = LexedFile::Token::Namespace;
retval[L"for"] = LexedFile::Token::For;
retval[L"while"] = LexedFile::Token::While;
retval[L"do"] = LexedFile::Token::Do;
retval[L"switch"] = LexedFile::Token::Switch;
retval[L"case"] = LexedFile::Token::Case;
retval[L"default"] = LexedFile::Token::Default;
retval[L"try"] = LexedFile::Token::Try;
retval[L"catch"] = LexedFile::Token::Catch;
retval[L"auto"] = LexedFile::Token::Auto;
retval[L"type"] = LexedFile::Token::Type;
retval[L"break"] = LexedFile::Token::Break;
retval[L"continue"] = LexedFile::Token::Continue;
retval[L"return"] = LexedFile::Token::Return;
retval[L"static"] = LexedFile::Token::Static;
retval[L"sizeof"] = LexedFile::Token::Sizeof;
retval[L"decltype"] = LexedFile::Token::Decltype;
retval[L"if"] = LexedFile::Token::If;
retval[L"else"] = LexedFile::Token::Else;
return retval;
}();
if (stack.empty())
return;
std::wstring token(stack.begin(), stack.end());
stack.clear();
if (reserved_words.find(token) != reserved_words.end()) {
l.tokens.push_back(reserved_words.find(token)->second);
return;
}
l.tokens.push_back(LexedFile::Identifier);
};
The compiler will accept it if I use the constructor directly not initialization, which seems very strange. Probably a compiler bug.
Calling the constructor with () instead of using = works just fine, so I'm marking this one as a compiler error.
I need a routine to randomly select records from ADAM (Active Directory Application Mode). Any advice to get me started on this task?
Use a DirectorySearcher filter with (objectClass=user) and pick a result at random might work. Something like ...
private static Random rnd = new Random();
private static DirectoryEntry GetRandomUser()
{
DirectoryEntry luckyGuy;
var de = new DirectoryEntry(/*conn string*/);
de.Username = /* your user */;
de.Password = /* your pass */;
// error handling and try-catch removed for clarity and brevity
var s = new DirectorySearcher( de );
s.Filter = "(objectClass=user)";
var res = s.FindAll();
if( res.Count > 0 )
{
var idex = rnd.Next(0, res.Count);
luckyGuy = res[idex].GetDirectoryEntry();
}
return luckyGuy;
}
Here's more on DirectorySearcher.