Page 9 of 23
Re: COMatePLUS version 1.1
Posted: Mon Sep 21, 2009 8:44 pm
by srod
Well, I cannot run your code because I do not have the relevant component installed.
However, I just tested with the agent demo and that works fine in a thread.
You have set the threadsafe switch I take it?
In itself COMatePLUS is threadsafe and I have applications which use it in threads and I have had no problems. COMatePLUS initialises the COM environment on any thread used to create an object so that is no problem. What you cannot do is pass objects between threads because PB initialises the COM environment to a single threaded apartment. Other than that COMate itself should give you no problem.
Try the following (set the threadsafe switch) :
Code: Select all
IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"
Procedure StartFilter(MyFilter$)
Define.COMateObject AgentObject, GenieObject
If ExamineDesktops()
cWidth = DesktopWidth(0) / 2 - 50
cHeight = DesktopHeight(0) / 4
EndIf
AgentObject = COMate_CreateObject("Agent.Control")
If AgentObject
AgentObject\SetProperty("Connected=#True")
AgentObject\Invoke("Characters\Load('Genie')")
GenieObject = AgentObject\GetObjectProperty("Characters('Genie')")
If GenieObject
GenieObject\Invoke("Show")
Delay(300)
GenieObject\Invoke("MoveTo("+StrU(cWidth)+", "+StrU(cHeight)+", 5000)")
GenieObject\Invoke("Play('Greet')")
GenieObject\Invoke("Speak('Hello, feel the ..Pure.. Power of Purebasic!')")
GenieObject\Invoke("Play('Reading')")
Delay(20000)
GenieObject\Invoke("Stop")
GenieObject\Invoke("Speak('Purebasic is a nice computer language.')")
Delay(6000)
MessageRequester("Agent", "Click ok to end")
GenieObject\Invoke("Play('Hide')")
Delay(3000)
GenieObject\Release()
Else
MessageRequester("Problem!", COMate_GetLastErrorDescription())
EndIf
AgentObject\Release()
Else
MessageRequester("Problem!", COMate_GetLastErrorDescription())
EndIf
EndProcedure
Procedure MyThread(null)
StartFilter("Y:\MyFilter.fll")
EndProcedure
OpenConsole()
CreateThread(@MyThread(),0)
PrintN("Press return to exit")
Input()
CloseConsole()
**EDIT : it seems to me that with the code you posted, you may have multiple threads trying to access the same file ("Y:\MyFilter.fll"). It could well be this which is screwing things up or something else to do with this component class. What happens if you only run a single thread (with the threadsafe switch!) Does it still crash?
Re: COMatePLUS version 1.1
Posted: Mon Sep 21, 2009 9:23 pm
by sverson
srod wrote:it seems to me that with the code you posted, you may have multiple threads trying to access the same file ("Y:\MyFilter.fll"). It could well be this which is screwing things up or something else to do with this component class. What happens if you only run a single thread (with the threadsafe switch!) Does it still crash?
This is just a little demo. My application takes the *.fll parameter from a array. "Y:\MyFilter.fll" will not be called twice at the same time. The *.fll execution does not crash. The error occurs on program end. There is no difference if the thread is called once or many times.
Re: COMatePLUS version 1.1
Posted: Mon Sep 21, 2009 9:28 pm
by srod
Afraid I have no answer other than to suggest that the problem is perhaps with the component itself.
Re: COMatePLUS version 1.1
Posted: Tue Sep 22, 2009 9:58 am
by srod
@sverson - I don't think this will make any difference, but try placing a CoUninitialize_() at the end of your thread function. Make it the last statement which executes in the thread.
To be honest though; the fact that you get the crash when the thread exits does indicate that the problem is with the component rather than with COMatePLUS. Does this component use any OLE functions (clipboard, drag-drop, etc) ? If so, then you will want to use OleInitialize_() at the top of the thread.
Re: COMatePLUS version 1.1
Posted: Thu Sep 24, 2009 11:18 am
by Kwai chang caine
Srod i don't know if you know
But OLLIVIER have make a great and good job for translate your wonderful help in french
He have put tle link today in the french forum
Perhaps you're interesting to know that
Thanks at OLLIVIER, because it's not always simple to add language and programming difficulty
http://www.purebasic.fr/french/viewtopi ... 47#p103747
Re: COMatePLUS version 1.1
Posted: Thu Sep 24, 2009 11:57 am
by sverson
srod wrote:..To be honest though; the fact that you get the crash when the thread exits does indicate that the problem is with the component rather than with COMatePLUS...
I don't get the crash when the thread exits - 1 to n threads are runing well. All executions run as expected. I get the crash on application end.
When I call the thread function directly without CreateThread() once or several times there is no error on application end. As soon as I have called the function via CreateThread() there will be this
Runtime Error 217 on application end.
CoUninitialize_() or OleInitialize_(0) does not help.

Re: COMatePLUS version 1.1
Posted: Thu Sep 24, 2009 12:15 pm
by srod
I would say that the most likely explanation is some dll corruption somewhere - either with the component itself or one of it's dependencies. Either that or the component dll has been unloaded after all the threads have finished (COM dll's can do that) but the system has not recognised that fact for some reason?
Try creating an instance of one of these objects in the main process, before creating any threads, and do not free it until after all the threads have finished.
I've read reports that outdated versions of the c++ runtime (Msvcrt.Dll) can also cause this problem.
If the crash persists, my advice would be to try an identical VB program. See if that crashes?
Re: COMatePLUS version 1.1
Posted: Thu Sep 24, 2009 12:49 pm
by sverson
Thanks srod.
I'll try to create instances before creating threads...
Re: COMatePLUS version 1.1
Posted: Mon Oct 12, 2009 5:58 pm
by besko
any Update for Beta 5?
Re: COMatePLUS version 1.1
Posted: Mon Oct 12, 2009 6:09 pm
by srod
Done.
@All : A very minor bug with COMatePLUS preventing compilation under pB 4.4 beta 4 onwards has been fixed. No change of version number required - just download the new version which will still run under PB 4.3 without a problem.
COMatePLUS version 1.1
Posted: Mon Nov 09, 2009 6:06 pm
by jear
@Help
How to translate this code snippet to COMate?
ActiveWorkbook.SaveAs FileName:="C:\Temp\xxxx.csv", FileFormat:=xlCSV, CreateBackup:=False, local:=True
My code doesn't work
Code: Select all
;/ Save excel sheet as .CSV
; code removed, see below
Re: COMatePLUS version 1.1
Posted: Mon Nov 09, 2009 6:36 pm
by srod
The SaveAs method you are attempting to translate appears to be a VBA macro!!! You cannot use that without VBA.
Use something like :
Code: Select all
arg$ = "SaveAs('" + Copy + "', " + Str(#xlCSV) + ")"
You will need to consult an OLE viewer in order to look in detail at the SaveAs method and its parameters.
Your code also has some dreadful memory leaks. E.g. you use ExcelSheet = ApplObj\GetObjectProperty("ActiveWorkbook") to overwite the value of ExcelSheet which contains an object reference. You must release the original object reference first! You can see the leak by firing up task manager as you will see that Excel remains running everytime you run your code!
Re: COMatePLUS version 1.1
Posted: Tue Nov 10, 2009 1:39 am
by jear
thanks srod
This code seems to work
Edit Nov 12, 2009 21:20 :
But in some cases, IMHO dependent on size and/or complexity of the source workbook, the EXCEL.EXE remains running.
The different forums are quite full with post, describing this problem.
After the ApplObj\Release, there is no message to get by COMate_GetLastErrorDescription()?
Is this correct?
Is there any way to get the hWnd or the processID of the created Excel ApplicationObject?
jear
Code: Select all
;/ jear * Nov 2009 * Save excel sheet as ....
IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"
;/ Excel FileFormat Constants
#xlNormal = -4143
#xlSYLK = 2
#xlCSV = 6
#xlCSVMac = 22
#xlCSVMSDOS = 24
#xlCSVWindows = 23
#xlText = -4158
#xlTextMac = 19
#xlTextMSDOS = 21
Procedure.s ExcelSheetSaveAs(Origin.s, Copy.s, FileFormat.l=#xlNormal, SheetNumber.l=1)
Protected ApplObj.COMateObject, ExcelBook.COMateObject, ExcelSheet.COMateObject
Protected result.s, args.s
ApplObj = COMate_CreateObject("Excel.Application")
If COMate_GetLastErrorCode() = #S_OK
ApplObj\SetProperty("Visible = #False")
ApplObj\SetProperty("DisplayAlerts = #False")
ExcelBook = ApplObj\GetObjectProperty("Workbooks\Open('" + Origin + "')")
If COMate_GetLastErrorCode() = #S_OK
ExcelBook\Invoke("Activate()")
ExcelSheet = ApplObj\GetObjectProperty("Sheets(" + Str(SheetNumber) + ")")
If COMate_GetLastErrorCode() = #S_OK
ExcelSheet\Invoke("Activate()")
args = "SaveAs('" + Copy + "'," + Str(FileFormat) + ")"
ExcelBook\Invoke(args)
If COMate_GetLastErrorCode() = #S_OK
ApplObj\Invoke("Quit()")
Else
result = "ERROR: " + COMate_GetLastErrorDescription() + " (" + args + ")"
EndIf
ExcelSheet\Release()
Else
result = "ERROR: Sheet Nr. " + Str(SheetNumber) + " does not exist"
EndIf
ExcelBook\Release()
Else
result = "ERROR: Cannot open EXCEL book" + Origin
EndIf
ApplObj\Release()
Else
result = "ERROR: Cannot open EXCEL application"
EndIf
ProcedureReturn result
EndProcedure
;/Test
SetCurrentDirectory(GetPathPart(ProgramFilename()))
Define.s home = GetCurrentDirectory(), result
;result = ExcelSheetSaveAs(home + "Test.xls", home + "Test2.xls", #xlNormal, 1)
;result = ExcelSheetSaveAs(home + "Test.xls", home + "Test.slk", #xlSYLK, 1)
result = ExcelSheetSaveAs(home + "Test.xls", home + "Test.csv", #xlCSV, 1)
If result : MessageRequester("ExcelSheetSaveAs", result, #MB_OK|#MB_ICONWARNING) : EndIf
End
Re: COMatePLUS version 1.1
Posted: Sun Dec 06, 2009 3:03 am
by leodh
Hello srod,
I am looking at setting up an application that get a group of questions from a file ( Word )
put them in a list, the user then selects which questions they want and a new word document is created hopfully in a table format ( Word )
that document is the emailed out, filled in and emailed back.
data is then read from the returned document and inserted into several different documents (txt).
Now the real question, I was thinking of using COMatePlus to do this am I barking up the wrong tree, if not where do I find the commands to control word ? or are there some demos I could look at.
Thanks
Leo
Re: COMatePLUS version 1.1
Posted: Sun Dec 06, 2009 11:56 am
by srod
I would say that COMatePLUS would give you as good a chance as any method of controlling MS Word to the extent that you are after. It would be quite some work though.