1

I have a VFP program that runs every so often.

I want to be able to call a program I created in C# that includes a string as a parameter.

I have tried alot of methods and none seem to work, I mostly just get a little "flash" and nothing happens

An example to go off:

RUN "C:\Desktop\C#Program" "Apples"

I want this to just be able to run from VFP. I have also tried to use ShellExecute and tried to call cmd.exe and pass in the parameter that way and nothing happened.

Error message

This is the error I get when I use the RUN command

derloopkat
  • 6,232
  • 16
  • 38
  • 45
CWeezy
  • 21
  • 4
  • What does "nothing happened" mean? It sounds like a dumb question but that could mean different things. It could mean that the program didn't run at all. It could mean that it ran but without your parameter. It could mean that your computer froze up. – Scott Hannen Jun 29 '20 at 20:51
  • @ScottHannen I hit run and it looks like something tried to happen but it just ends, the program finishes but my C# program never actually ran. Its like the VFP program tried to run it and it error'd instantly but it doesnt say it did. – CWeezy Jun 29 '20 at 21:10
  • You said that you tried with ShellExecute, but that error message is irrelevant to ShellExecute call. Also "a little "flash" and nothing happens" might actually mean, it worked, did its job and exited. Would you care to share your VFP and C# codes? – Cetin Basoz Jun 30 '20 at 11:22
  • @CetinBasoz Yea the error message is from when I tried to use the RUN Command, My fault I should have placed that picture message better. The program also doesnt run because my C# program creates a PDF and prints it. Also the sample code I put above is pretty much what I am doing, its just running the .exe with a string parameter. My C# program runs fine when I use the same line of code in a cmd window – CWeezy Jun 30 '20 at 12:42

2 Answers2

1

You can call your executable like this and check the return value (Message).

#Define SW_HIDE             0
#Define SW_SHOWNORMAL       1
#Define SW_NORMAL           1
#Define SW_SHOWMINIMIZED    2
#Define SW_SHOWMAXIMIZED    3
#Define SW_MAXIMIZE         3
#Define SW_SHOWNOACTIVATE   4
#Define SW_SHOW             5
#Define SW_MINIMIZE         6
#Define SW_SHOWMINNOACTIVE  7
#Define SW_SHOWNA           8
#Define SW_RESTORE          9
#Define SW_SHOWDEFAULT      10
#Define SW_FORCEMINIMIZE    11
#Define SW_MAX              11

Local lcFileName, lcResult

lcFileName = Fullpath("c:\MyFolder\MyCSharpExecutable.exe")

lcResult = ShellExec(m.lcFileName,"Apples","",SW_SHOWNORMAL)
If !Empty(m.lcResult) && error
    Messagebox(m.lcResult)
Endif

Function ShellExec
    Lparameters tcExecutable,tcParams,tcWorkingDir,tnShowType,tcOperation

    Declare Long ShellExecute In "shell32.dll" ;
        long HWnd, String lpszOp, ;
        string lpszFile, String lpszParams, ;
        string lpszDir, Long nShowCmd
    tcOperation  = Iif(Empty(m.tcOperation), 'Open', m.tcOperation)
    tcExecutable = Iif(Empty(m.tcExecutable), '', m.tcExecutable)
    tcParams     = Iif(Empty(m.tcParams), '', m.tcParams)
    tcWorkingDir = Iif(Empty(m.tcWorkingDir), '', m.tcWorkingDir)
    tnShowType   = Iif(Type('m.tnShowType') # 'N', SW_SHOWNORMAL, m.tnShowType)
    Local lnResult, lcError
    lcError = ''
    lnResult = ShellExecute(0,m.tcOperation,m.tcExecutable,m.tcParams,m.tcWorkingDir,m.tnShowType)
    If !( m.lnResult > 32 ) && Error
        lcError = GetShExecErrorMsg(m.lnResult)
    Endif
    Return m.lcError
Endfunc

Function GetShExecErrorMsg
    Lparameters tnErrNum
    Local Array aErrors[1]
    Local lcMessage, lcErrors,lnErrors,ix

    TEXT to m.lcErrors noshow
0,The operating system is out of memory or resources. \n
2,The specified file was not found. \n
3,The specified path was not found. \n
11,The .exe file is invalid (non-Win32® .exe or error in .exe image). \n
5,The operating system denied access to the specified file.  \n
27,The file name association is incomplete or invalid. \n
30,The DDE transaction could not be completed because
other DDE transactions were being processed. \n
29,The DDE transaction failed. \n
28,The DDE transaction could not be completed because the request timed out. \n
32,The specified dynamic-link library was not found.  \n
31,There is no application associated with the given file name extension.
This error will also be returned if you attempt to print a file that is not printable. \n
8,There was not enough memory to complete the operation. \n
26,A sharing violation occurred. \n
    ENDTEXT
    Clear
    lnErrors = Alines(aErrors,m.lcErrors,.T.,'\n')
    For ix=1 To m.lnErrors
        If ( Val(Chrtran(Left(aErrors[m.ix],;
                At(',',aErrors[m.ix])-1),Chr(13)+Chr(10),'')) = m.tnErrNum )
            lcMessage = Substr(aErrors[m.ix],At(',',aErrors[m.ix])+1)
            Exit
        Endif
    Endfor
    If Empty(m.lcMessage)
        lcMessage = 'An unspecified error occurred.'
    Endif
    Return m.lcMessage
Endfunc
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
1
Local lcFileName, lcResult


lcFileName = Fullpath("c:\MyFolder\MyCSharpExecutable.exe")

lcResult = ShellExec(m.lcFileName,"Chase","",1)

Function ShellExec
    Lparameters tcExecutable,tcParams,tcWorkingDir,tnShowType,tcOperation

    Declare Long ShellExecute In "shell32.dll" ;
        long HWnd, String lpszOp, ;
        string lpszFile, String lpszParams, ;
        string lpszDir, Long nShowCmd
    tcOperation  = Iif(Empty(m.tcOperation), 'Open', m.tcOperation)
    tcExecutable = Iif(Empty(m.tcExecutable), '', m.tcExecutable)
    tcParams     = Iif(Empty(m.tcParams), '', m.tcParams)
    tcWorkingDir = Iif(Empty(m.tcWorkingDir), '', m.tcWorkingDir)
    tnShowType   = Iif(Type('m.tnShowType') # 'N', 1, m.tnShowType)
    Local lnResult, lcError
    lcError = ''
    lnResult = ShellExecute(0,m.tcOperation,m.tcExecutable,m.tcParams,m.tcWorkingDir,m.tnShowType)
Endfunc

I took out all of the other stuff which was not needed when I got it to run. This is all I have and it runs the way I want it to. Thank you!

CWeezy
  • 21
  • 4