variant/object in c#

recently i made a topic cause of a problem to use the GetFieldData, i got a reply on a dutch forum to use the stringbuilder :slight_smile: now it works BUT cause its a string, decimal/dates/etc are not showns properly, strings got a ÿ in front it(code field). numbers got a square in front of it (char 255)(codefield, value’s in % are shown as a @(decimal field), and some other bugs ----- [DllImport(“cfront.dll”, CallingConvention = CallingConvention.Cdecl)] private static extern int DBL_GetFieldData(System.Text.StringBuilder dst, int dstsize, int hTable, int record, int fieldnr); ----- case “lr” : //list records new style dTable = new DataTable(“List Records”); int hrecord = DBL_AllocRec(htable); //Fields(columns) aanmaken int afield = 0; int i = DBL_NextField(htable,0); //first field string type = “”; while ( i != 0 ) { switch (DBL_FieldType(htable,i)) { case (35072) : type = “System.String” ; break; //code case (11520) : type = “System.String”; break; //text case (34048) : type = “System.Boolean”; break; //boolean case (34560) : type = “System.Int32”; break; //integer case (12800) : type = “System.String”; break; //decimal case (35584) : type = “System.String”; break; //option case (11798) : type = “System.String”; break; //dateformula case (11776) : type = “System.DateTime”; break; //date default: type = “System.String”; Console.WriteLine(“fieldtype " + DBL_FieldType(htable,i).ToString() + " niet gevonden.”); break; } dColumn = new DataColumn(Marshal.PtrToStringAnsi(DBL_FieldName(htable,i)),System.Type.GetType(type)); dTable.Columns.Add(dColumn); afield++; i = DBL_NextField(htable,i); } //data erin if (DBL_FindRec(htable, hrecord ,"-") ) //eerste record { System.Text.StringBuilder sbuilder = new System.Text.StringBuilder(100); dRow = dTable.NewRow(); int dteller = DBL_NextField(htable,0); //nr van field for (int k = 0 ; k < afield ; k++) //van field 0 tot laatste field { DBL_GetFieldData(sbuilder, sbuilder.MaxCapacity, htable,hrecord,dteller); string tdata = sbuilder.ToString(); if (tdata != “”) { switch (DBL_FieldType(htable,dteller)) { case (35072) : dRow[k] = tdata.Substring(1,tdata.Length-1); break; //code case (11520) : dRow[k] = tdata; break; //text case (34048) : dRow[k] = true; break; //boolean //nog uitzoeken case (34560) : dRow[k] = Convert.ToInt32(tdata,16); break; //integer case (12800) : dRow[k] = tdata; break;//Convert.ToDecimal(tdata); break; //decimal case (35584) : dRow[k] = tdata; break; //option case (11798) : dRow[k] = tdata; break; //dateformula case (11776) : dRow[k] = Convert.ToDateTime(tdata); break; //date } } dteller = DBL_NextField(htable,dteller); } dTable.Rows.Add(dRow); while ( DBL_NextRec(htable, hrecord, 1) != 0 ) //elke volgende record { dRow = dTable.NewRow(); dteller = DBL_NextField(htable,0); for (int k = 0 ; k < afield ; k++) { DBL_GetFieldData(sbuilder, sbuilder.MaxCapacity, htable,hrecord,dteller); string tdata = sbuilder.ToString(); if (tdata != “”) { switch (DBL_FieldType(htable,dteller)) { case (35072) : dRow[k] = tdata.Substring(1,tdata.Length-1); break; //code case (11520) : dRow[k] = tdata; break; //text case (34048) : dRow[k] = true; break; //boolean //nog uitzoeken case (34560) : dRow[k] = Convert.ToInt32(tdata,16); break; //integer case (12800) : dRow[k] = tdata; break;//Convert.ToDecimal(tdata); break; //decimal case (35584) : dRow[k] = tdata; break; //option case (11798) : dRow[k] = tdata; break; //dateformula case (11776) : dRow[k] = Convert.ToDateTime(tdata); break; //date } } dteller = DBL_NextField(htable,dteller); } dTable.Rows.Add(dRow); } } DBL_FreeRec(hrecord); break; (dont go like [?] on the case statements, those were changed each time i debugged (testing purposes)) the GetFieldData function returns and variant. but if i specify de dllimport so it returns an object. create the object like "object blaat = new object(); and then call the function with the object blaat gives a exception. u guys got some ideas/examples how to catch the variant and transform it into an string/date etc? thanks. EDIT: changed to code to normal text, much easier reading

Code field is DBL_Type_ALPHA in cfront. First byte is used for sorting purposes. I don’t know how in C# but in C++ _variant_t object is used for mapipulations.

thx for the alpha tip, missed it in the ref but its there :slight_smile: the _variant_t object is not availeble in C#, c# got the ‘object’ but keep getting nullreference when i try to use it Object obj1 = new Object(100); makes a object with 100 bytes i think?

i made a small program with 2 textfields and 6 buttons. This is what i tried and the strange results i got: //textBox1 is the input and textBox2 the output bcd_2_str [DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void DBL_BCD_2_Str(System.Text.StringBuilder _string, int strLength, string deciStr); //bcd to string System.Text.StringBuilder sbuilder = new System.Text.StringBuilder(35); DBL_BCD_2_Str(sbuilder,35,textBox1.Text); textBox2.Text = sbuilder.ToString(); input: “1234” result: “0,00000000000000000000000000343332” str_2_bcd [DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void DBL_Str_2_BCD(System.Text.StringBuilder _string, string dateStr); //string to bcd System.Text.StringBuilder sbuilder = new System.Text.StringBuilder(35); DBL_Str_2_BCD(sbuilder,textBox1.Text); textBox2.Text = sbuilder.ToString(); input: “1234” other input: 0,00000000000000000000000000343332 (result bcd_2_str) result: “C” output: % [Dlbcd_2_double[/b] //note i use the stringbuilder cause if i used as 1param a double i didnt get anything [DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void DBL_BCD_2_Double(System.Text.StringBuilder _value, string bcdstr); //bcd to double System.Text.StringBuilder sbuilder = new System.Text.StringBuilder(35); DBL_BCD_2_Double(sbuilder,textBox1.Text); textBox2.Text = Convert.ToString(sbuilder.ToString()); input: “1234” result: “OyØ1Øÿp:” [Dldouble_2_bcd[/b] [DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void DBL_Double_2_BCD( string bcdstr, double _value); //double to bcd string getal = ""; DBL_Double_2_BCD(getal,Convert.ToDouble(textBox2.Text)); textBox2.Text = getal; input: “1234” result: error inputstring was not in a correct format [Dldate_2_str[/b] [DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void DBL_Date_2_Str(System.Text.StringBuilder _string, int strLength, string dateStr); //date to string System.Text.StringBuilder sbuilder = new System.Text.StringBuilder(35); DBL_Date_2_Str(sbuilder,35,textBox1.Text); textBox2.Text = sbuilder.ToString(); input: any date result: “U11-06-1697” ← always gives this in return [Dlstr_2_date[/b] [DllImport("cfront.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void DBL_Str_2_Date(System.DateTime date, string dateStr); //string to date System.DateTime datum = new System.DateTime(); DBL_Str_2_Date(datum,textBox1.Text); textBox2.Text = Convert.ToString(datum); input: Any date result: object reference not set to an instance of an object Where does it go wrong and what is a solution?