Issue calling third party C code

jakethedog317

New member
Joined
Nov 21, 2014
Messages
1
Programming Experience
3-5
Hello I have a third party api that I am using within a C# Windows form

I am having trouble with one piece of it where I am trying to increment through a ptr to a structure that has a ptr to ten structures within it and was hoping for some advice This is my code for a function call - In a nutshell it pulls 10 records at a time and then is supposed to loop and get the next ten. The variable top is what keeps track of the number of records processed. I think but not sure and this is really part of my problem. It should go back into the call when it loops to grab the next ten records starting with the value of top. The num value is the number of total records that come back from the call after the call. Example if I know I have 50 records and top starts at 0 and grab the first 10 then will be 50 num and then it will loop and top should be 10 and so num should be 40. It will do the loop until it reaches the end of num

during each iteration I grab the proper value from the structures within the structure and plop it to the grid. I have sample C code that I included below along with my structures and code. Can someone take a look at this and give advice on what doing wrong and can do differently.

All help is appreciated

Given that here is my information

This is the setup of the structures

 [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=4)]
    public class PRGDIR3
    {
        public PRGDIR3_data dir1 = new PRGDIR3_data();
        public PRGDIR3_data dir2 = new PRGDIR3_data();
        public PRGDIR3_data dir3 = new PRGDIR3_data();
        public PRGDIR3_data dir4 = new PRGDIR3_data();
        public PRGDIR3_data dir5 = new PRGDIR3_data();
        public PRGDIR3_data dir6 = new PRGDIR3_data();
        public PRGDIR3_data dir7 = new PRGDIR3_data();
        public PRGDIR3_data dir8 = new PRGDIR3_data();
        public PRGDIR3_data dir9 = new PRGDIR3_data();
        public PRGDIR3_data dir10= new PRGDIR3_data();
    } /* In case that the number of data is 10 */






 [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=4)]
    public class PRGDIR3_data
    {
        public int     number ;
        public int     length ;
        public int     page ;
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst=52)]
        public string  comment= new string(' ',52) ;
        public DIR3_MDATE mdate = new DIR3_MDATE();
        public DIR3_CDATE cdate = new DIR3_CDATE();
    }

This is my code

public const int Bufsize = 1000

// num is intially set to Bufsize but after first call it is set to the returned records

private void GetNCNumberList()
        {
            try
            {
                unsafe
                {     
                    int top = 0;  Should keep track of the number of processed records brought back from the api call
                    int i;
                    short num;
                    int currentnumber;
                    Focas1.PRGDIR3 prg = new Focas1.PRGDIR3();   <--- This is where I instantiate the third party structures
                    DataGridViewRow row = new DataGridViewRow();
                    row.CreateCells(dataGridView1);
                    dataGridView1.ColumnCount = 2;
                    dataGridView1.Rows.Clear();
                    
                    do
                    {
                        i = 0;
                        currentnumber = 0;
                        int numberloop;
                        string value;
                        
                        num = Bufsize;
                 
                        Focas1.focas_ret ret =
                            (Focas1.focas_ret)Focas1.cnc_rdprogdir3(cncHandle, 1, ref top, ref num, prg);
                        if (ret == Focas1.focas_ret.EW_NUMBER)
                        {
                            break;
                        }


                        if (ret == Focas1.focas_ret.EW_NUMBER)
                        {
                            break;
                        }
                        if (num == 0)
                        {
                            break;
                        }
                  
                        if (num > 10)
                            numberloop = 9;
                        else
                            numberloop = num - 1;
                        do
                        {
                            value = i.ToString();
                            switch (value)
                            {
                                case "0":
                                {
                                    currentnumber = prg.dir1.number;
                                    dataGridView1.Rows.Add(prg.dir1.number.ToString(), prg.dir1.comment);
                                    break;
                                }
                                case "1":
                                {
                                    currentnumber = prg.dir2.number;
                                    dataGridView1.Rows.Add(prg.dir2.number.ToString(), prg.dir2.comment);
                                    break;
                                }
                                case "2":
                                {
                                    currentnumber = prg.dir3.number;
                                    dataGridView1.Rows.Add(prg.dir3.number.ToString(), prg.dir3.comment);
                                    break;
                                }
                                case "3":
                                {
                                    currentnumber = prg.dir4.number;
                                    dataGridView1.Rows.Add(prg.dir4.number.ToString(), prg.dir4.comment);
                                    break;
                                }
                                case "4":
                                {
                                    currentnumber = prg.dir5.number;
                                    dataGridView1.Rows.Add(prg.dir5.number.ToString(), prg.dir5.comment);
                                    break;
                                }
                                case "5":
                                {
                                    currentnumber = prg.dir6.number;
                                    dataGridView1.Rows.Add(prg.dir6.number.ToString(), prg.dir6.comment);
                                    break;
                                }
                                case "6":
                                {
                                    currentnumber = prg.dir7.number;
                                    dataGridView1.Rows.Add(prg.dir7.number.ToString(), prg.dir7.comment);
                                    break;
                                }
                                case "7":
                                {
                                    currentnumber = prg.dir8.number;
                                    dataGridView1.Rows.Add(prg.dir8.number.ToString(), prg.dir8.comment);
                                    break;
                                }
                                case "8":
                                {
                                    currentnumber = prg.dir9.number;
                                    dataGridView1.Rows.Add(prg.dir9.number.ToString(), prg.dir9.comment);
                                    break;
                                }
                                case "9":
                                {
                                    currentnumber = prg.dir10.number;
                                    dataGridView1.Rows.Add(prg.dir10.number.ToString(), prg.dir10.comment);
                                    break;
                                }
                                default:
                                {
                                    MessageBox.Show("Invalid");
                                    break;
                                }
                            }
                            int index = this.dataGridView1.Rows.Count;
                            index ++;


                            i++;
                            top = i;
                        } while (i <= numberloop);
                       // top = currentnumber;
                        top = top + 1;




                    } while (num != 0);
                   
                   


                }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message");


        }

This is a sample from C that I have to work off of - and obviously I am not properly converting it.

The following program reads all registration information of NC program,
and displays program number list.

C#:
#include "fwlib32.h"


#define BUFSIZE 100


void example()
{
        PRGDIR3 prg[BUFSIZE];
        short i, num;
        short top = 0;


        do {
                num = BUFSIZE;
                ret = cnc_rdprogdir3( h, 0, &top, &num, prg );
                if ( ret == EW_NUMBER ) {
                        break;
                }
                if ( ret ) {
                        printf( "ERROR: %d\n", ret );
                        break;
                }
                for ( i = 0 ; i < num ; i++ ) {
                        printf( "O%d\n", prg[i].number );
                }
                top = prg[num-1].number + 1;
        } while ( num >= BUFSIZE );
}
 
Last edited by a moderator:
Back
Top Bottom