COM object with BMax ?
BlitzMax Forums/BlitzMax Programming/COM object with BMax ?
| ||
Is it possible to make a reference to a COM object (Windows) with BlitzMax ? My COM object is COM S7Prosim (S7wspsmx.dll). In Visual Studio, I made this : ADD REFERENCE --> Siemens S7Prosim COM Object and in the source code : Private WithEvents S7ProSim As New S7PROSIMLib.S7ProSim After, I have access to Method and Events of this COM Object (like : STDMETHOD(Connect)(), HRESULT PLCSimStateChanged(BSTR NewState), ...) I do not know if I was clear enough in my expliquations. Sorry for my English, I'm French ! Thanks |
| ||
Hiya, Yes you can. There are a variety of ways, and its usually easier that you have a dll as there may be a function in there to create the base COM object. If not then you can use the windows CoCreateInstance to create it. I'm not familiar with what objects derive from which in the S7ProSim ibrary but here is a general layout to get you started... Import Pub.Win32 Extern"win32" 'equivalent of STDMETHOD Type S7ProSim Extends IUnknown '?? The docs will tell you the correct inheritance Method Connect() Method PLCSimStateChanged:Int(NewState:Byte Ptr) 'More methods EndType Type S7ProSimEvents Extends S7ProSim '?? The docs will tell you the correct inheritance Method [...] 'More methods EndType EndExtern The order of the Methods inside the Type (COM Object) is very very important and has to match the originals exactly. Any more help feel free to ask as I feel this reply may be a little vague? |
| ||
Thank you for your help ! |
| ||
If you want you can download the S7ProSim COM object documentation (in pdf) here : http://support.automation.siemens.com/WW/llisapi.dll?query=s7prosim&func=cslib.cssearch&content=adsearch%2Fadsearch.aspx&lang=en&siteid=cseus&objaction=cssearch&searchinprim=&nodeid99=&x=24&y=14 Just click on the download link ! It's a object of Siemens Automation (for simulate a Industrial PLC : Programable Logic Controller). |
| ||
Thats great. Thanks! What project are you creating to use this? |
| ||
Looking at the docs you may need to wrap it using some c++. You can also do that with BMax too :-) |
| ||
I don't know how a wrapper work using c++ ? If it's not too complicated and if you want, can you help me with a blitzmax program example for this Com object ? I want to make a process simulator (industrial machine, tool machine, ...). I have already make that but in Visual Basic.net (more easy than c++) but now I would like to make that project in BlitzMax (new for me) which makes easy the graphical part !!! Thanks. |
| ||
For col (Dave) : Where do you live ? I live in France ! |
| ||
Yeah sure, Where can I download that .dll file so I can get things started for you to get it working properly? I've looked on the web-site but couldn't find anything that would like look it. Is it freely available? I'm currently living in the UK, just east of London. |
| ||
Ok, thank you very much !!! The link to download the dll : http://dl.free.fr/bzBdIZiuO I know a little london, I went for my job more times. I live in Mulhouse, near Germany ! |
| ||
Strange... The link opens. It asks to enter 2 words in the image but the image is blank!?!?!? Could you email to me please. Email address is in my profile which you can view by clicking on my username. Ive only driven through France on my journey to Spain. Its a BIG country. Pretty too. London is OK. When I visit London I always look at it as though it needs a really good wash!! The buildings are soooo dirty. But its ok I guess. :-) |
| ||
Ok, I've give you a mail ! |
| ||
Hiya, I got the .dll in the mail. Cool. I'll take a good look over this weekend. This will be interesting as I'm confident this will need a c++ wrapper. Did you obtain it with an install process? or as a standalone .dll file? Reason is I can already see it has some other dll dependencies which I'll resolve to get it working. Last edited 2012 |
| ||
Yes, the .dll come from a install process. I don't know if you can make it work like that ! Monday, I have to go to Mexico City for three weeks. The time difference will not be the same! (-7 hours !). I will answer post in the forum with one day late (probably !) |
| ||
Hiya, This code will get you started. It will give you an instance of the IS7ProSim COM object. There are many many ways to do it. I've included another way to do it thats commented out. Also, I've not finished putting the correct function parameters for the IS7ProSim interface or the IS7ProSimEvents interface - I've left it for you to do :-) However, I did start to so that you have a form of reference to use for help. |
| ||
Hope youre having fun in Mexico :-) |
| ||
Ouah ! Incredible !!! Very very good work ! Whenever I have time, I will test and try to understand what you wrote ;-) I have a lot of job at Mexico !!! Thank you for all the good work !!! |
| ||
Dave, Your code seems to work ! I used the Connect method : OK ! I would like to use the SetState and GetState method but I don't know how to use them ? I'm not familiar with ptr. Can you help me with an example ? Thanks ! |
| ||
No worries. According to the docs those functions utilitse a BSTR. You can use this code to allocate and free BSTR :- Strict Import "-loleaut32" Extern Function SysAllocString:Byte Ptr(str:Byte Ptr)"Win32" Function SysFreeString(bstr:Byte Ptr)"Win32" EndExtern 'To Send a BSTR Local myBSTR:Byte Ptr myBSTR = SysAllocString("myBSTR String") myS7ProSim.SetState(myBSTR) 'To recieve a BSTR myBSTR = myS7ProSim.GetState() Print String.FromCString(myBSTR) 'Free it SysFreeString(myBStr) I don't know the library so when using GetState I'm not sure if its for you to free the returned BSTR or if the S7ProSim library will free it internally. The docs may let you know. If not then check the memory usage in the task manager to see if its goes up for your app each time you use that function. If the memory usage does goes up then you should use SysFreeString to free any returned BSTRs. It's more than likely than you should free it yourself, but do check to avoid problems. EDIT:- For the BSTR and VARIANT functions you can find all the documentation from MS on the MSDN here Any problems feel free to ask. Last edited 2012 |
| ||
Thanks I've this compilation error : Missing function parameter 'pVal' with myBSTR = myS7ProSim.GetState() ??? |
| ||
Oops!! Thats my bad! The GetState() should look like GetState:Byte Ptr() You need to go through all the methods and make sure that the function parameters all match up to what the library is expecting. I only did the first couple to give to a start. You need to finish the rest. As I don't have a working library, I can't double check anything. |
| ||
ok, thanks |
| ||
>> You need to go through all the methods and make sure that the function parameters all match up to what the library is expecting : YOU'RE RIGHT ! It's my job ;-) I've a EXCEPTION_ACCESS_VIOLATION with GetState ?!!! the code : Method GetState:Byte Ptr() ... Local myBSTRState:Byte Ptr myBSTRState = pIS7ProSim.GetState() <--- HERE is the problem ! Print String.FromCString(myBSTRState) |
| ||
You may need an already initialised BSTR?? EXCEPTION_ACCESS_VIOLATION is usually when youre code tries to access a memory address outside of what your apps allowance. Which would make me think about the address of the myBSTRState variable will initially be $00000000 ( and outside yours apps memory range access allowance ). A BSTR is a simple ( but different ) string structure. Unfortunately, to go any further I'd need a working library so as to find out why its not working :( |
| ||
Local myBSTRState:Byte Ptr = SysAllocString( myS7ProSim.GetState() ) Unlikely but you never know :-) Forget that, I was just brainstorming! Although it IS worth a try to save making a huge BSTR wrapper. Last edited 2012 |