LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

access dictionary objects from C# dll in CVI

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);

0 Kudos
Message 1 of 5
(5,013 Views)

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

0 Kudos
Message 2 of 5
(5,001 Views)

I have created a .net controller. That is what the function calls I am using are from.

 

0 Kudos
Message 3 of 5
(4,982 Views)

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

http://stackoverflow.com/questions/5102436/how-to-marshal-collection-in-c-sharp-to-pass-to-native-c-...

 

The solution is the same for ANSI C in this case.

______________
Florian Abry
Inside Sales Engineer, NI Germany
0 Kudos
Message 4 of 5
(4,793 Views)

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();
}

 

Message 5 of 5
(4,768 Views)