Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fredz0004 · Jan 04, 2016 at 10:19 PM · error messageplugindll

Marshal error while using an external DLL, can anyone provide pointers or wrongdoing?

Hi,

I am an automation engineer and I would like to build some troubleshooting scenarios in unity for my coworkers. I came accross a DLL Com object (on windows) Interop.S7PROSIMLib which lets you call methods and read/write in C# to the PLCSim which is great because Siemens PLC Step7 is great to write automation programs. I tested with Visual Studio and it works great.

alt text

And this is the code that calls those methods, the reference to the DLL is within the solution explorer in visual studio and thus no need to use using.

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 
 namespace WindowsFormsApplication1
 {
     public partial class Form1 : Form
     {
         public S7PROSIMLib.S7ProSim PLCSimConn = new S7PROSIMLib.S7ProSim();
 
         public Form1()
         {
             InitializeComponent();
         }
 
         private void button_Connect_Click(object sender, EventArgs e)
         {
             PLCSimConn.Connect();
             label_CPUState.Text = PLCSimConn.GetState();
             label_ScanMode.Text = PLCSimConn.GetScanMode().ToString();
         }
     }
 }


And in a c sharp script in unity I have this:

 using UnityEngine;
 using System.Collections;
 using S7PROSIMLib;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     private S7PROSIMLib.S7ProSimClass ps;
     // Use this for initialization
     void Start () {
 
         ps = new S7PROSIMLib.S7ProSimClass();
         ps.Connect(); 
     }
     
 }

alt text

I cannot remember how did the DLL became Interop the original name is S7PROSIMLib, I imagine after I dragged and dropped to the assets/plugins folder. After that it was referenced in visual studio as interop.XXX and I was no longer getting the console error in unity of missing reference or using XXX in script.

I got excited when no more console errors where showing. I ran, and then I received a COMException which I have no clue where to fix or even where to begin. This is the error I get in unity something to do with Marshal.

 COMException
 System.Runtime.InteropServices.Marshal.ThrowExceptionForHR (Int32 errorCode) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs:1031)
 System.__ComObject.Initialize (System.Type t) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/__ComObject.cs:103)
 (wrapper remoting-invoke-with-check) System.__ComObject:Initialize (System.Type)
 Mono.Interop.ComInteropProxy.CreateProxy (System.Type t) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/Mono.Interop/ComInteropProxy.cs:108)
 System.Runtime.Remoting.RemotingServices.CreateClientProxyForComInterop (System.Type type) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Remoting/RemotingServices.cs:588)
 System.Runtime.Remoting.Activation.ActivationServices.CreateProxyForType (System.Type type) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Remoting.Activation/ActivationServices.cs:234)
 NewBehaviourScript.Start () (at Assets/Scripts/NewBehaviourScript.cs:11)


Can anyone that has experience with this error give me some pointers. Maybe this is not doable. And this is a link to the API for S7ProSim Documentation

Side note: I went through dozens of posts, and links online for using DLL but most of the links are user created DLL's.

Thanks for your help.

objectbrowser.jpg (225.1 kB)
plcsimconnect.jpg (70.9 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cjdev · Jan 05, 2016 at 12:48 AM 0
Share

It could be due to .Net version incompatibility as Unity uses a fairly old implementation of $$anonymous$$ono still I think (according to this post at least, info is pretty vague in general) which targets mostly .Net 2.0/3.5.

avatar image fredz0004 · Jan 06, 2016 at 12:09 AM 0
Share

I found a video on YouTube called nexusS7 automation published last year. Which showed unity3d interfaced with plcsim so there is hope. I found some useful documentation. I for once misinterpreted managed vs unmanaged dll. I will write some test code and I will post it here if it works.

https://msdn.microsoft.com/en-us/library/ms973872.aspx

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fredz0004 · Jan 08, 2016 at 02:32 PM

For anyone that might need help with this. Siemens PLCSim COM type library only works with 32bit Unity Editor.

The solution was actually quite easy after lots of trial and error, and many more reading through the forums and what not. I used tlbimp.exe to generate a type library DLL, then I placed that into the assets/plugins folder also note on unity3d editor properties that the new DLL is treated as managed code. Note that this particular DLL doesn’t work on 64bit Unity Editor and it threw a COMException. However the new type library DLL works fine in 32bit Unity Editor, I imagine that somewhere in the DLL there is an instruction specifying to work only in 32bit.

I documented everything on a repository here https://github.com/fredz0003/myS7ProSimLib I also placed the managed DLL for future use so you don't go to the trouble I went.

Below is a sample code:

 using UnityEngine;
 using myS7ProSimLib;
 
 public class TestNative : MonoBehaviour {
 
     /*
     * Used tlbimp.exe to generate library DLL
     * place new generated DLL on assets/plugins
     * also note that the DLL is treated as managed code
     */
     public S7ProSimClass ps;
     public bool input_0_0;
     
 
     void Start () {
         ps = new S7ProSimClass();
 
         ps.Connect();
         print("State " + ps.GetState());
         ps.SetScanMode(ScanModeConstants.ContinuousScan);
 
         // Here we pass the ref as an obj, since WriteInputPoint method
         // can take bit, word, dwords, as addresses ref obj can take the
         // for of bool, int, float, etc.
         object refInput0_0 = input_0_0;
                                         
         ps.WriteInputPoint(0, 0, ref refInput0_0);
     }
     
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Superlux · Feb 02, 2016 at 11:47 PM 0
Share

@fredz0004 Ooo forget the last message about help, I'm starting to use your managed library in unity 5 32 bits and it recognices it. It's great, your work is being very helpful.

avatar image Eco-Editor · Jul 13, 2017 at 09:13 AM 0
Share

Hi @fredz0004

Note that this particular DLL doesn’t work on 64bit Unity Editor and it threw a CO$$anonymous$$Exception

I have a DLL file that supposed to give markers on ECG recorder. And whenever I have it call the function for the marker it gives me the CO$$anonymous$$Exception.

I have Unity 64bit. Do you think that it means my dll is not compatible with the program, is there a way to change it in the dll or should I download unity 32bit? should I compile a new dll to be compatible with 64bit?

avatar image
0

Answer by josephgan87 · Aug 28, 2018 at 08:50 AM

alt text

I had this issue when using coreScanner COM from zebra scanner currently I stuck dont know what to do.

Any one can advise what I done wrong?


capture.jpg (234.3 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Harinezumi · Aug 28, 2018 at 09:18 AM 0
Share

Please don't ask a new question as an answer, even if the topic is related. Ins$$anonymous$$d, create a new question and refer to this one for info.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnauthorizedAccessException when accessing DLL 1 Answer

Native c++ plugin on android 0 Answers

How to link to scripts in the Package Manager when building a dll. 0 Answers

Problem with dll plugin 0 Answers

How do you use a class from a .dll file? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges