01. november 2009 - 21:48Der er
11 kommentarer og 1 løsning
exe med returværdi
Fra Excel kan man køre en exe-fil: Sub Runcalc() Dim RetVal RetVal = Shell("C:\WINDOWS\CALC.EXE", 1) End Sub
Hvis exe-filen er lavet i C#, hvorledes kan man så få den til at returner en værdi til Excel? Jeg har brug for at læse comporten og har lavet et lille program, der kan det. Men hvordan returnerer jeg den aflæste værdi til Excel?
Sådan ? using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Environment.Exit(77);
} } }
og herefter;
Sub Runcalc() Dim RetVal RetVal = Shell("c:\Documents and Settings\mko\My Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe", 1) End Sub
Kan ikke få 77 lagt i RetVal. 4732 kommer retur i stedet....
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
'This Sub Main() Dim lTaskID As Long, lPID As Long, lExitCode As Long, sAppDir As String Const INFINITE = &HFFFFFFFF ' Infinite timeout Const SYNCHRONIZE = &H100000 Const STILL_ACTIVE = 0 Const STANDARD_RIGHTS_REQUIRED = &HF0000 Const PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
sAppDir = Application.path If Right$(sAppDir, 1) <> "\" Then sAppDir = sAppDir & "\" End If ' MsgBox "This application will now start another application (out of process)." & vbNewLine & _ ' "Please enter a return value and click Close" & vbNewLine & _ ' "This display the value entered in the other application...", vbInformation 'Shell Application lTaskID = Shell("c:\Documents and Settings\mko\My Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe", 1) 'Get process handle lPID = OpenProcess(PROCESS_ALL_ACCESS, True, lTaskID) If lPID Then 'Wait for process to finish 'Note, you must now enter a value in the form and click close. Call WaitForSingleObject(lPID, INFINITE) 'Get Exit Process If GetExitCodeProcess(lPID, lExitCode) Then 'Received value ActiveCell.Value = lExitCode Else MsgBox "Failed: " & DLLErrorText(Err.LastDllError), vbCritical End If Else MsgBox "Failed: " & DLLErrorText(Err.LastDllError), vbCritical End If lTaskID = CloseHandle(lPID) End Sub
'Purpose : Return the error message associated with LastDLLError 'Inputs : lLastDLLError The error number of the last DLL error (from Err.LastDllError) 'Outputs : Returns the error message associated with the DLL error number
Public Function DLLErrorText(ByVal lLastDLLError As Long) As String Dim sBuff As String * 256 Dim lCount As Long Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100, FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_STRING = &H400 Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000, FORMAT_MESSAGE_IGNORE_INSERTS = &H200 Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0) If lCount Then DLLErrorText = Left$(sBuff, lCount - 2) 'Remove line feeds End If
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.