【问题标题】:Visual FoxPro run a C# Program with ParametersVisual FoxPro 运行带参数的 C# 程序
【发布时间】:2020-06-29 20:24:42
【问题描述】:

我有一个经常运行的 VFP 程序。

我希望能够调用我在 C# 中创建的包含字符串作为参数的程序。

我尝试了很多方法,但似乎都没有奏效,我大多只是得到一点“闪光”而没有任何反应

举个例子:

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

我希望它能够从 VFP 运行。我也尝试使用ShellExecute 并尝试调用cmd.exe 并以这种方式传递参数,但没有任何反应。

这是我使用 RUN 命令时遇到的错误

【问题讨论】:

  • “什么都没发生”是什么意思?这听起来像是一个愚蠢的问题,但这可能意味着不同的事情。这可能意味着程序根本没有运行。这可能意味着它运行但没有您的参数。这可能意味着您的计算机死机了。
  • @ScottHannen 我点击运行,它看起来像是试图发生的事情但它刚刚结束,程序完成但我的 C# 程序从未真正运行过。就像 VFP 程序试图运行它,它立即出错,但它没有说它确实。
  • 您说您尝试使用 ShellExecute,但该错误消息与 ShellExecute 调用无关。此外,“一点点“闪光”,什么都没有发生”实际上可能意味着它起作用了,完成了它的工作并退出了。您愿意分享您的 VFP 和 C# 代码吗?
  • @CetinBasoz 是的,错误消息来自我尝试使用 RUN 命令时,我的错我应该更好地放置该图片消息。该程序也没有运行,因为我的 C# 程序创建了一个 PDF 并打印它。此外,我在上面放置的示例代码几乎就是我正在做的,它只是运行带有字符串参数的 .exe。当我在 cmd 窗口中使用同一行代码时,我的 C# 程序运行良好

标签: c# parameters exe visual-foxpro


【解决方案1】:

您可以像这样调用您的可执行文件并检查返回值(消息)。

#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

【讨论】:

  • 此代码有效!我将发布我用来运行它的确切内容。
【解决方案2】:
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

当我让它运行时,我取出了所有其他不需要的东西。这就是我所拥有的一切,它按照我想要的方式运行。谢谢!

【讨论】:

  • 没有不需要的东西 :) 当它失败时他们会帮助你“调试”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2023-03-24
  • 1970-01-01
  • 2019-02-28
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多