J'ai essayé ton code mais j'ai une erreur :
[C++ Erreur] Unit1.cpp(41): E2285 Impossible de trouver une correspondance pour 'string::basic_string(unsigned char *,unsigned int)'
Version imprimable
J'ai essayé ton code mais j'ai une erreur :
[C++ Erreur] Unit1.cpp(41): E2285 Impossible de trouver une correspondance pour 'string::basic_string(unsigned char *,unsigned int)'
Toujours la meme erreur ...'string::basic_string(unsigned char *,unsigned int)'Citation:
essaye en faisant un cast :
static_cast ou reinterpret_cast
cout<< string(static_cast<unsigned char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str()<< endl;
Voici les différents constructeur de string (autrement dit de basic_string<char>) :
donc normalement, avec ceci :Code:
1
2
3
4
5
6
7
8 string ( ); string ( const string& str ); string ( const string& str, size_t pos, size_t n = npos ); string ( const char * s, size_t n );//celui qui peut marcher avec la solution de Minnesota string ( const char * s ); string ( size_t n, char c ); template<class InputIterator> string (InputIterator begin, InputIterator end);
cout<< string(static_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str()<< endl;
ça devrait marcher.
Cannot cas from 'unsigned char*' to 'char *'Citation:
cout<< string(static_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str()<< endl;
Je comprend vraiment pas comment fair :S
Oui je viens d'essayer, sa compile bien, mais lors de l'execution, il ne ce passe rienCitation:
reinterpret_cast à la place de static_cast (unsigned char et char sont considérés comme des types sans relation)
J'ai vient d'installé le service snmp sur mon PC pour voir de plus près le problème que tu rencontres, j'ai fait cette ébauche et émis une requête sur lui même, ben avec ce que je t'es indiqué précédemment ça fonctionne : Assure-toi juste que ton oid correspond à une chaine.
Après, bien sur, il faut tout virer aux profits de quelque chose de plus propre à base de ostringstream...
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 #include <windows.h> #include <Snmp.h> //+ lib Snmpapi #include <Mgmtapi.h> // + lib mgmtapi #include <iostream> #include <string> #include <vector> #include <cstdio> using namespace std; #define TRACE printf /*********************************************************************** * SnmpUtilPrintAsnAny (SNMPAPI.@) - * http://source.winehq.org/source/dlls/snmpapi/main.c */ VOID WINAPI SnmpUtilPrintAsnAny2(AsnAny *any) { unsigned int i; TRACE("(%p)\n", any); switch (any->asnType) { case ASN_NULL: TRACE("Null value\n"); return; case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return; case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return; case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return; case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return; case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return; case ASN_COUNTER64: { TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart); return; } case ASN_OCTETSTRING: { TRACE("String "); for (i = 0; i < any->asnValue.string.length; i++) TRACE("%c", any->asnValue.string.stream[i]); TRACE("\n"); return; } case ASN_IPADDRESS: { TRACE("IpAddress "); if (any->asnValue.string.length < 4) { TRACE("Invalid\n"); return; } for (i = 0; i < 4; i++) { TRACE("%u", any->asnValue.string.stream[i]); if (i < 3) TRACE("."); } TRACE("\n"); return; } case ASN_BITS: { TRACE("Bits "); for (i = 0; i < any->asnValue.string.length; i++) { TRACE("0x%02x", any->asnValue.string.stream[i]); if (i < any->asnValue.object.idLength - 1) TRACE(" "); } TRACE("\n"); return; } case ASN_OPAQUE: { TRACE("Opaque "); for (i = 0; i < any->asnValue.string.length; i++) { TRACE("0x%02x", any->asnValue.string.stream[i]); if (i < any->asnValue.object.idLength - 1) TRACE(" "); } TRACE("\n"); return; } case ASN_OBJECTIDENTIFIER: { TRACE("ObjectID "); for (i = 0; i < any->asnValue.object.idLength; i++) { TRACE("%u", any->asnValue.object.ids[i]); if (i < any->asnValue.object.idLength - 1) TRACE("."); } TRACE("\n"); return; } default: { TRACE("Invalid type %d\n", any->asnType); return; } } } int main(int argc, char* argv[]){ vector<string> SNMPErrorInfoInCodes; SNMPErrorInfoInCodes.push_back("0 SNMP_ERRORSTATUS_NOERROR:\nThe agent reports that no errors occurred during transmission"); SNMPErrorInfoInCodes.push_back("1 SNMP_ERRORSTATUS_TOOBIG:\nThe agent could not place the results of the requested SNMP operation in a single SNMP message."); SNMPErrorInfoInCodes.push_back("2 SNMP_ERRORSTATUS_NOSUCHNAME:\nThe requested SNMP operation identified an unknown variable."); SNMPErrorInfoInCodes.push_back("3 SNMP_ERRORSTATUS_BADVALUE:\nThe requested SNMP operation tried to change a variable but it specified either a syntax or value error."); SNMPErrorInfoInCodes.push_back(""); SNMPErrorInfoInCodes.push_back(""); SNMPErrorInfoInCodes.push_back(""); SNMPErrorInfoInCodes.push_back(""); SNMPErrorInfoInCodes.push_back(""); SNMPErrorInfoInCodes.push_back(""); SnmpVarBindList snmpVarList; AsnInteger errorStatus,errorIndex; LPSNMP_MGR_SESSION session; //Initialisation snmpVarList snmpVarList.list = NULL; snmpVarList.len = 1; cout<< "realloc"<< endl; snmpVarList.list = (SnmpVarBind*) \ realloc(snmpVarList.list, sizeof(SnmpVarBind) * snmpVarList.len); cout<< "SnmpMgrOpen"<< endl; session = SnmpMgrOpen("127.0.0.1","public",1000,3); if (!session){ cout<< "no_session"<< endl; return 1; } cout<< "session_ok"<< endl; //.1.3.6.1.2.1.1.1.0 for system description //.1.3.6.1.2.1.1.4.0 for system contact //.1.3.6.1.2.1.1.5.0 for system name SnmpMgrStrToOid(".1.3.6.1.2.1.1.5.0", &snmpVarList.list->name); cout<< "SnmpMgrStrToOid_ok"<< endl; //Envoi de la requete if (!SnmpMgrRequest(session,SNMP_PDU_GET, &snmpVarList, &errorStatus, &errorIndex)){ cout<< "SnmpMgrRequest function fails"<< endl; SnmpMgrClose(session); return 1;} cout<< "errorStatus: "<< SNMPErrorInfoInCodes[errorStatus]<< endl; cout<< endl; cout<< "by SnmpUtilPrintAsnAny: "<< endl; SnmpUtilPrintAsnAny(&snmpVarList.list->value); cout<< endl; cout<< "by string_constructor only if any->asnType==ASN_OCTETSTRING: "<< endl; AsnAny* any= &snmpVarList.list->value; if (any->asnType==ASN_OCTETSTRING) cout<< "String "<< string(reinterpret_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str() << endl; cout<< endl; cout<< "by SnmpUtilPrintAsnAny2: "<< endl; SnmpUtilPrintAsnAny2(&snmpVarList.list->value); cout<< endl; /* if (errorStatus==SNMP_ERRORSTATUS_NOERROR){ cout<< "no error occurred during transmission"<< endl; SnmpUtilPrintAsnAny(&snmpVarList.list->value); } //*/ cout<< "SnmpMgrClose"<< endl; SnmpMgrClose(session); return 0; }
Salut tout le monde
Merci pour ton essai minnesota, lorceque j'éssai ton code cher moi sur mon switch,Cette parti la du code fais planter le programme =s, et lorceque je le met en commentaire, la fonction SnmpUtilPrintAsnAny affiche bien la chaine, mais la fonction SnmpUtilPrintAsnAny2 n'affiche pas la chaine, je te joint une copie d'ecran de ma console.Code:
1
2
3
4 if (any->asnType==ASN_OCTETSTRING) cout<< "String "<< string(reinterpret_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str() << endl;
J'éssai avec un entier qui represente le numéro de port, avec la fonction SnmpUtilPrintAsnAny de la bibligotheque j'affiche bien "interger32 34" alors qu'avec la fonction SnmpUtilPrintAsnAny2 de minnesota j'affiche "interger32 0". j'ai vraiment besoin de garder ce numéro de port en mémoire
Essaye d'appeler SnmpUtilPrintAsnAny en dernier, il n'est pas impossible qu'il y ait eu une réinitialisation suite à son appel. Essaye aussi de faire le test depuis un autre PC à base de Windows XP par exemple. Comme il ne m'est pas possible de reproduire ton problème, je ne vois pas comment t'aider.
Essaie ceci et montre le résultat, sait-on jamais :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 #include <windows.h> #include <Snmp.h> //+ lib Snmpapi #include <Mgmtapi.h> // + lib mgmtapi #include <iostream> #include <string> #include <vector> #include <cstdio> using namespace std; #define TRACE printf /*********************************************************************** * SnmpUtilPrintAsnAny (SNMPAPI.@) - * http://source.winehq.org/source/dlls/snmpapi/main.c */ VOID WINAPI SnmpUtilPrintAsnAny2(AsnAny *any) { unsigned int i; TRACE("(%p)\n", any); switch (any->asnType) { case ASN_NULL: TRACE("Null value\n"); return; case ASN_INTEGER32: TRACE("Integer32 %d\n", any->asnValue.number); return; case ASN_UNSIGNED32: TRACE("Unsigned32 %u\n", any->asnValue.unsigned32); return; case ASN_COUNTER32: TRACE("Counter32 %u\n", any->asnValue.counter); return; case ASN_GAUGE32: TRACE("Gauge32 %u\n", any->asnValue.gauge); return; case ASN_TIMETICKS: TRACE("Timeticks %u\n", any->asnValue.ticks); return; case ASN_COUNTER64: { TRACE("Counter64 %x%08x\n", (DWORD)(any->asnValue.counter64.QuadPart>>32),(DWORD)any->asnValue.counter64.QuadPart); return; } case ASN_OCTETSTRING: { TRACE("String "); for (i = 0; i < any->asnValue.string.length; i++) TRACE("%c", any->asnValue.string.stream[i]); TRACE("\n"); return; } case ASN_IPADDRESS: { TRACE("IpAddress "); if (any->asnValue.string.length < 4) { TRACE("Invalid\n"); return; } for (i = 0; i < 4; i++) { TRACE("%u", any->asnValue.string.stream[i]); if (i < 3) TRACE("."); } TRACE("\n"); return; } case ASN_BITS: { TRACE("Bits "); for (i = 0; i < any->asnValue.string.length; i++) { TRACE("0x%02x", any->asnValue.string.stream[i]); if (i < any->asnValue.object.idLength - 1) TRACE(" "); } TRACE("\n"); return; } case ASN_OPAQUE: { TRACE("Opaque "); for (i = 0; i < any->asnValue.string.length; i++) { TRACE("0x%02x", any->asnValue.string.stream[i]); if (i < any->asnValue.object.idLength - 1) TRACE(" "); } TRACE("\n"); return; } case ASN_OBJECTIDENTIFIER: { TRACE("ObjectID "); for (i = 0; i < any->asnValue.object.idLength; i++) { TRACE("%u", any->asnValue.object.ids[i]); if (i < any->asnValue.object.idLength - 1) TRACE("."); } TRACE("\n"); return; } default: { TRACE("Invalid type %d\n", any->asnType); return; } } } int main(int argc, char* argv[]){ vector<string> SNMPErrorInfoInCodes; //J'avais commencé avec un vector, mais un map est plus indiqué... SNMPErrorInfoInCodes.push_back("\ 0 SNMP_ERRORSTATUS_NOERROR:\n\ The agent reports that no errors occurred during transmission"); SNMPErrorInfoInCodes.push_back("\ 1 SNMP_ERRORSTATUS_TOOBIG:\n\ The agent could not place the results of the requested SNMP operation in a \ single SNMP message."); SNMPErrorInfoInCodes.push_back("\ 2 SNMP_ERRORSTATUS_NOSUCHNAME:\n\ The requested SNMP operation identified an unknown variable."); SNMPErrorInfoInCodes.push_back("\ 3 SNMP_ERRORSTATUS_BADVALUE:\n\ The requested SNMP operation tried to change a variable but it specified \ either a syntax or value error."); SNMPErrorInfoInCodes.push_back("\ 4 SNMP_ERRORSTATUS_READONLY:\n\ The requested operation tried to change a variable that was not allowed to \ change according to the community profile of the variable."); SNMPErrorInfoInCodes.push_back("\ 5 SNMP_ERRORSTATUS_GENERR:\n\ An error other than one of those listed here occurred during the requested \ operation."); SNMPErrorInfoInCodes.push_back("\ UNKNOWNERROR\n"); SNMPErrorInfoInCodes.push_back("\ SNMP_MGMTAPI_TIMEOUT:\n\ The request timed-out."); SNMPErrorInfoInCodes.push_back("\ SNMP_MGMTAPI_SELECT_FDERRORS:\n\ Unexpected error file descriptors indicated by the Windows Sockets select \ function."); SnmpVarBindList snmpVarList; AsnInteger errorStatus,errorIndex; LPSNMP_MGR_SESSION session; //Initialisation snmpVarList snmpVarList.list= 0; snmpVarList.len= 1; cout<< "realloc"<< endl; snmpVarList.list= (SnmpVarBind*) \ realloc(snmpVarList.list, sizeof(SnmpVarBind) * snmpVarList.len); int timeOutInmilliseconds= 3000; int numberOfRetries= 2; cout<< "SnmpMgrOpen"<< endl; session = SnmpMgrOpen( "127.0.0.1", "public", timeOutInmilliseconds, numberOfRetries ); if (!session){ cout<< "no_session"<< endl; return 1; } cout<< "session_ok"<< endl; //.1.3.6.1.2.1.1.1.0 for system description //.1.3.6.1.2.1.1.4.0 for system contact //.1.3.6.1.2.1.1.5.0 for system name SnmpMgrStrToOid(".1.3.6.1.2.1.1.5.0", &snmpVarList.list->name); cout<< "SnmpMgrStrToOid_ok"<< endl; //Envoi de la requete if (!SnmpMgrRequest(session,SNMP_PDU_GET, &snmpVarList, &errorStatus, &errorIndex)){ cout<< "SnmpMgrRequest function fails"<< endl; cout<<"errorStatus: "<< errorStatus<< " - errorIndex: "<<errorIndex<< endl; switch(::GetLastError()){ case SNMP_MGMTAPI_TIMEOUT: cout<< SNMPErrorInfoInCodes[7]<< endl; break; case SNMP_MGMTAPI_SELECT_FDERRORS: cout<< SNMPErrorInfoInCodes[8]<< endl; break; default: cout<< SNMPErrorInfoInCodes[6]<< endl; } SnmpMgrClose(session); return 1;} cout<< "errorStatus: \n"<< SNMPErrorInfoInCodes[errorStatus]<< endl; cout<< endl; AsnAny* any= &snmpVarList.list->value; cout<< endl; cout<< "by string_constructor only if any->asnType==ASN_OCTETSTRING: "<< endl; if (any->asnType==ASN_OCTETSTRING) cout<< "String " << string(reinterpret_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str() << endl; cout<< endl; cout<< "balayage (unicode?) only if any->asnType==ASN_OCTETSTRING: "<< endl; if (any->asnType==ASN_OCTETSTRING){ cout<< "String ("<< any->asnValue.string.length<< ") "<< flush; for (int i= 0; i < 30; i++) cout<< any->asnValue.string.stream[i]<< flush; cout<< endl; } cout<< endl; cout<< "by SnmpUtilPrintAsnAny2: "<< endl; SnmpUtilPrintAsnAny2(&snmpVarList.list->value); cout<< endl; cout<< "by SnmpUtilPrintAsnAny: "<< endl; SnmpUtilPrintAsnAny(&snmpVarList.list->value); cout<< endl; cout<< "SnmpMgrClose"<< endl; SnmpMgrClose(session); return 0; }
Salut,
Je viens d'essayer ton morceau de code malheureusement, cela ne fonction pas.
Lorceque je l'execute, tout va bien, aucun code d'erreur mais lorceque j'arive sur
Le programme s'arrete.Code:
1
2
3
4
5 if (any->asnType==ASN_OCTETSTRING) cout<< "String " << string(reinterpret_cast<char*>(any->asnValue.string.stream), any->asnValue.string.length).c_str() << endl;
Pareil pour le test suivant, et pour SnmpUtilPrintAnsAny2.
Si je met tout en commentaire, ma chaine s'affiche bien avec SnmpUtilPrintAsnAny.
Je ne peux pas trop essayer sur un windows Xp, je vais essayer tt de suite ...
Voila si tu a une autre idée.Merci
le probleme vien du faite que tout est a 0, je joint une copi d'ecran ou j'ai arreter le programme juste avant d'appeler SnmpUtilPrintAsnAny2 et ou j'inspect ma variable tout est a 0
C'est bizarre comme problème, mais comme je ne peux pas le reproduire, je ne vois pas trop. Tu peux éventuellement tester un utilitaire comme snmputil, sinon je peux aussi te filer la version compilée du programme pour tester histoire d'être sûr que c'est pas ton edi-rad qui est en cause...
Ouai c'est vraiment bizzar. Je veux bien ta version compiler que j'essai sur mon environnement
Y'a trois utilitaires différents, dont la version compilée.
Dans le snmp_info, quand tu lance le getinfo.cmd, c'est avec le programme en c++ que tu m'a envoyer?? parceque cela marche très bien cher moi
Oui, avec snmp_info.exe
Tu peux m'envoyé le code source complet de ce programme stp, parceque cela marche