Announcement

Collapse
No announcement yet.

C to PB Help needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C to PB Help needed

    Hi, C code below seems straight forward but I am getting stuck on the
    correct declaration of the argv parameter. any help please ?


    Code:
    int __declspec(dllexport)	__stdcall	DBFtoCSV_Converter(HWND hwnd, int argc, char *argv[]);
    
    int
    main()
    {
    	int		n=0;
    	char	*params[10];
    
    	params[n++]=strdup(__argv[0]);
    	params[n++]=strdup("source.dbf");
    	params[n++]=strdup("target.csv");
    	params[n++]=strdup("/ansi");
    	params[n++]=strdup("/overwrite=0");
    
    	return DBFtoCSV_Converter(NULL, n, params);
    }

  • #2


    Thank you, Google.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Thanks Michael, kinda figured it was an array pointer, but string or asciiz?.
      whats the best way to declare it? or just declare as dword ?

      N.

      Comment


      • #4
        The command line arguments is an array of string, so declare it as BYREF STRING.
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #5
          ok, you'll have to excuse my ignorance, am I delcaring as
          byref argv as string ptr and passing a pointer to the first element?
          or delcaring as byref argv() as string ptr ?


          ta,
          N.

          Comment


          • #6
            No. You have to declare it as BYREF argv AS STRING and pass a pointer to the first element of the array, that you will do passing argv(0).
            Forum: http://www.jose.it-berater.org/smfforum/index.php

            Comment


            • #7
              >>DBFtoCSV_Converter(HWND hwnd, int argc, char *argv[]);

              >BYREF argv AS STRING


              "BYREF as String" passes the address of an OLE string handle, "char * argv[]" wants the address of the first byte of a (small a) array of null-terminated strings.

              Code:
              DECLARE FUNCTION DBFtoCSV_Converter LIB "whatver.dll"  [ALIAS "whatever"] (BYVAL hwnd AS LONG, BYVAL argc AS LONG, BYVAL argv aS DWORD) AS LONG
               .......
                 LOCAL arvg AS STRING 
                 argv =  Param1 & $NUL & PARAM2 & $NUL [& paramN & $NUL]...
                
              
                 CALL DbftoCSV_Converter (hWnd, arcg, BYVAL STRPTR (argv))
              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                "char * argv[]" wants the address of the first byte of a (small a) array of null-terminated strings.
                No, it wants a pointer to an array of string pointers.
                argv[] is an array and char * is a pointer to a byte string.
                OLE strings are also null-terminated, so can be used for the purpose.
                Last edited by José Roca; 5 Feb 2009, 11:37 AM.
                Forum: http://www.jose.it-berater.org/smfforum/index.php

                Comment


                • #9
                  Code:
                  _argv, _wargvHeader File 
                  
                  stdlib.h 
                  
                  Syntax 
                  
                  extern char **_argv; 
                  
                  extern wchar_t ** _wargv
                  Oops, I missed that second '*' preceding the _argv

                  However, passing "BYREF AS STRING" should still be passing the address of the string descriptor.

                  But since what is at VARPTR(ole string) is the address of the string data, I guess it will work OK. (But that does not mean I would be real comfy doing that.... at least not without some extensive commenting in my source code).


                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    dBase III/IV source code

                    Neil, it looks like you might be doing work with dBase files. If you would like a dBase III/IV file viewer (listview output) with complete source code, drop me a line at ian[dot][email protected][dot]bc[dot]ca. (one of my more requested projects).

                    regards, Ian.
                    :) IRC :)

                    Comment


                    • #11
                      If there is an OLE provider for dBase (and I'm sure there is), converting a dbase database to a CSV file is probably less than 20 lines of PB code using ADO.
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Neil,
                        Maybe not as close as you would like but maybe a step forward as to how the parameter is passed to you.

                        Get that Data Type in the source code forums.

                        I will admit, its just a beginning, and as Jose will point out, numeric data is sometimes incorrect (but correct as in functional)

                        aka, from what I got from PB, if I declare byte, but the passed value is byte, my guessing is the passed data is LONG (unless the value is beyond the parameters of BYTE)

                        Still working on it, but maybe something useful
                        Engineer's Motto: If it aint broke take it apart and fix it

                        "If at 1st you don't succeed... call it version 1.0"

                        "Half of Programming is coding"....."The other 90% is DEBUGGING"

                        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                        Comment


                        • #13
                          Thanks Ian, I have a lot of accounting data to convert over the next two months (data is all VFP dbf). I did start coding the extract stuff but have problems getting the correct field count, when I add the 263 byte buffer into the equation it works for some files but not others, when I remove it from the equation it works for the others. They are all reporting the correct VFP file type in the header....
                          Normally I just export the data usinf CDBFW and run 20 or so queries in excel to give me the import data but this time I have 7 companies to convert all one April 1st (new financial year) so I am trying to automate everything.

                          Will try methods from Jose and see where it gets me, failing that will post my own code and see if anyone can pick whats wrong

                          Comment


                          • #14
                            The ultimate purpose of these variables...

                            int argc, char *argv[]

                            is to extract the command line arguments passed to the program. I doubt that in this code the programmer created those variables. So, if you are going to be converting a C prog to PB, you'll be using PB's Command$ syntax to retrieve whatever is passed in as a command tail. If the command tail has several arguements you'll have to parse them. Here is a simple example of how those variables are used in a C or C++ program named 'testing' if you execute the program like this...

                            C:\testing one two three [ENTER]

                            Code:
                            C:\Code>testing one two three
                            
                            argc = 4
                            
                            0       testing
                            1       one
                            2       two
                            3       three
                            Code:
                            #include <stdio.h>
                            #include <stdlib.h>
                            
                            int main(int argc, char* argv[])
                            {
                             printf("\nargc = %u\n\n",argc);
                             for(int i=0;i<argc;i++)
                                 printf("%u\t%s\n",i,argv[i]);
                             getchar();
                            
                             return 0;
                            }
                            Fred
                            "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                            Comment


                            • #15
                              Thanks Fred, in this case it is a dll with a single function, see c++ calling below:

                              Code:
                              int __declspec(dllexport)	__stdcall	DBFtoCSV_Converter(HWND hwnd, int argc, char *argv[]);
                              
                              int
                              main()
                              {
                              	int     n=0;
                              	char    *params[10];
                              
                              	params[n++]=strdup(__argv[0]);
                              	params[n++]=strdup("source.dbf");
                              	params[n++]=strdup("target.csv");
                              	params[n++]=strdup("/ansi");
                              	params[n++]=strdup("/overwrite=0");
                              
                              	return DBFtoCSV_Converter(NULL, n, params);
                              }

                              Comment


                              • #16
                                argv[0] is the name of the program that was executed; in my case above you can see it was 'testing.exe' but I just executed it as 'testing'. Other than that I'm not sure what is going on there, that is, why that would be a valuable piece of info to pass on to the function call at bottom?

                                return DBFtoCSV_Converter(NULL, n, params);

                                params would point to the beginning of the null terminated char* array, so DBFtoCSV converter would end up getting the string name of the host program that loaded the dll? Or am I off track? Other than that, none of the other info in argc or argv[] is being passed to DBFtoCSV_Converter.
                                Last edited by Fred Harris; 7 Feb 2009, 03:56 PM.
                                Fred
                                "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                                Comment


                                • #17
                                  HI Fred,
                                  In PB it would be something like this (using NULL sparated args),
                                  just cant get it to work.

                                  Code:
                                  DECLARE FUNCTION DBFtoCSV_Converter LIB "dbf2csv_d.dll" ALIAS "DBFtoCSV_Converter"(BYVAL hwnd AS LONG, BYVAL argc AS LONG, BYREF argv AS STRING) AS LONG
                                  
                                  FUNCTION PBMAIN () AS LONG
                                    DIM sArgs AS STRING 
                                    LOCAL lRet AS LONG
                                    LOCAL lCount AS LONG
                                  
                                   sArgs = "cl.dbf" & CHR$(0) & "cl.csv" & CHR$(0) & "/REMCRLF=1" & CHR$(0) & "/OVERWRITE=1" & CHR$(0) & "/QUOTES=1" & CHR$(0) & "/HEADER=1"
                                  
                                   nCount = 6
                                   lRet =  DBFtoCSV_Converter( BYVAL %NULL , BYVAL nCount,  sArgs)
                                  
                                  
                                  END FUNCTION
                                  Last edited by Neil J Hosgood; 8 Feb 2009, 02:24 PM.

                                  Comment


                                  • #18
                                    Try

                                    Code:
                                    DIM sArgs (5) AS STRING
                                    
                                    sArgs(0) = "cl.dbf"
                                    sArgs(1) = "cl.csv"
                                    sArgs(2) = "/REMCRLF=1"
                                    sArgs(3) = "/OVERWRITE=1"
                                    sArgs(4) = "/QUOTES=1"
                                    sArgs(5) = "/HEADER=1"
                                    
                                    nCount = 6
                                    lRet =  DBFtoCSV_Converter( BYVAL %NULL , BYVAL nCount,  sArgs(0))
                                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                                    Comment


                                    • #19
                                      That did the trick, Many thanks.

                                      N.
                                      Last edited by Neil J Hosgood; 8 Feb 2009, 03:44 PM.

                                      Comment

                                      Working...
                                      X