08-09-2013 03:13 PM
I am try to use a C# snmp dll in CVI. I have things working but I am at a loss as how to get the data out from the "snmp.Get" function since the result is a .net dictionary type. What do I do with the "dict" variable in my CVI code?
The C# code sample is:
string host = "localhost"; string community = "public"; SimpleSnmp snmp = new SimpleSnmp(host, community); if (!snmp.Valid) { Console.WriteLine("SNMP agent host name/ip address is invalid."); return; } Dictionary<Oid,AsnType> result = snmp.Get(SnmpVersion.Ver1, new string[] { ".1.3.6.1.2.1.1.1.0"} ); if (result == null) { Console.WriteLine("No results received."); return; } foreach (KeyValuePair kvp in result) { Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(), SnmpConstants.GetTypeName(kvp.Value.Type), kvp.Value.ToString()); }
My sample code is:
char *oid[] = {{"1.3.6.1.2.1.1.1.0"}};
System_Collections_Generic_Dictionary_T2 dict;
status = Initialize_SnmpSharpNet();
status = SnmpSharpNet_SimpleSnmp__Create_2 (&hSnmp,"localhost", "public", 0);
status = SnmpSharpNet_SimpleSnmp_Get_1 (hSnmp, 0x0, oid, 1, &dict, 0);
08-10-2013 06:10 PM
You can create CVI .Net controller (Tools >Create .Net controller) and use it with CVI.
I Think is not possible to use DLL C# without .Net controller
Bye
08-12-2013 08:12 AM
I have created a .net controller. That is what the function calls I am using are from.
02-04-2014 07:15 AM
Hi lesc,
C# Collections (like Dictionary) can not be understood by non-.NET Languages. There is also no marshalling functions to cast them to unmanaged datatypes. If you need to use those data in CVI, you will need to cast them in some kind of wrapper DLL first. Alternatively, if they have to be passed as parameter in a CVI DLL Call from a C# environment, you can first cast the collection to array (for example) and them marshall the array into an unmanaged array.
some more informations can be found on stackoverflow:
How to marshal collection in c# to pass to native (C++) code
The solution is the same for ANSI C in this case.
02-04-2014 08:15 PM
The System.Collections.Dictionary type is in the mscorlib .NET assembly - .NET v2 and higher. You need to use a CVI .NET wrapper for this assembly. There is already a shipping wrapper for this assembly in the CVI samples/dotnet folder - you can just use that in your CVI project; generating a wrapper for mscorlib assembly will take a very long time as it is a huge assembly.
The following is CVI code that shows how to print a Dictionary<int,string> - hope this helps. See samples/dotnet/GenericList and samples/dotnet/Hashtable for similar CVI code to use .NET generic List and non-generic HashTable types.
#include <mscorlib2.h> /* Add samples/dotnet/mscorlib2.fp to your project. */
#include <toolbox.h> /* Add toolbox.fp to your project. */
void main(void)
{
int error = 0;
System_Collections_Generic_Dictionary_T2 dictionary = 0;
System_Collections_Generic_Dictionary_T2_Enumerator enumerator = 0;
System_Collections_Generic_KeyValuePair_T2 kvp = 0;
int key, hasItems;
char *value = 0;
errChk(Initialize_mscorlib());
// Create dictionary of int to string and add some key-value pairs to it.
errChk(System_Collections_Generic_Dictionary_T2__Create(&dictionary, "System.Int32", "System.String", 0));
for (int i = 0; i < 10; ++i)
{
char valueBuf[64], *valuePtr = valueBuf;
snprintf(valueBuf, sizeof valueBuf, "%d", i);
errChk(System_Collections_Generic_Dictionary_T2_Add(dictionary, &i, &valuePtr, 0));
}
// Iterate and print the items in the dictionary.
errChk(System_Collections_Generic_Dictionary_T2_GetEnumerator(dictionary, &enumerator, 0));
do
{
errChk(System_Collections_Generic_Dictionary_T2_Enumerator_MoveNext(enumerator, &hasItems, 0));
if (hasItems)
{
errChk(System_Collections_Generic_Dictionary_T2_Enumerator_Get_Current(enumerator, &kvp, 0));
errChk(System_Collections_Generic_KeyValuePair_T2_Get_Key(kvp, &key, 0));
errChk(System_Collections_Generic_KeyValuePair_T2_Get_Value(kvp, &value, 0));
fprintf(stdout, "Key: %d, Value: %s\n", key, value);
CDotNetFreeMemory(value); value = 0;
CDotNetDiscardHandle(kvp); kvp = 0;
}
} while (hasItems);
Error:
if (error < 0)
fprintf(stderr, "Error: %d, %s\n", error, GetGeneralErrorString(error));
if (kvp) CDotNetDiscardHandle(kvp);
if (enumerator) CDotNetDiscardHandle(enumerator);
if (dictionary) CDotNetDiscardHandle(dictionary);
Close_mscorlib();
puts("\nPress enter to exit...");
getchar();
}