Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Let me try this a bit different maybe I can get somebody to bite ....:-)
Below Code will work if called direct - but will fail if called with reflection ??
Any Ideas why that would be - again looking at the debugger in the data view, the data looks the "same" regardless on how you call it ..but fails to execute if you use reflection
TEST_COPY is my structure.
What am I Missing ???? It has to be something simple ....

<br />
 Public Sub DLL_SUB(ByVal Par As Object)<br />
        Dim x As Integer<br />
        For x = 0 To NEW_STRUCTURE.Length - 1<br />
            ' The following code will fail<br />
            NEW_STRUCTURE(x) = CType(Par(x), TEST_COPY)<br />
<br />
        Next<br />
    End Sub<br />

I'm working on Adding DLL's to my main Program and stumbled over the following Issue:
- When trying to copy a structure from the main program to the DLL I'm getting an exception
- I have no problem transferring data to the DLL - it's when I try to copy over the complete structure is when I get the error
- At the point, where the code breaks you can "clearly" see that the correct data is there -it's just the conversion from the object to the structure is failing
Deceleration of Structure
Public Structure TEST_COPY
        Public V_STRING As String ' same as KdmCognexSetup(Index)
        Public v_Integer As Integer
        Public v_Bool As Boolean
    End Structure

' final code inside DLL that fails (DLL called trough reflection )
Public Sub DLL_SUB(ByVal MethodeName As String, ByVal Par As Object)
        Select Case MethodeName
            Case "TEST"
                Dim x As Integer
                For x = 0 To NEW_STRUCTURE.Length - 1
                    ' This line fails whil trying to convert the object to the structure [TEST_COPY]
                    ' You can clearly see the data is there - it's just the conversion fails
                    ' What's missing ?
                    NEW_STRUCTURE(x) = CType(Par(x), TEST_COPY)
                'Exit Sub
                'End Try
        End Select
    End Sub

Converting the Structure to an object for and back inside the Main Program is no issue at all = therefore I assume my problem has to do with reflection

I wrote a small test program to trouble shoot the issue - but I'm not sure how I can add the Zip file containing the code to this question

How can I add a file to the question ?

Updated 23-Jul-13 21:10pm
Sergey Alexandrovich Kryukov 24-Jul-13 0:46am    
This post create a sense of some kind of schizophasia:

"Copy a structure from the main program to the DLL" does not seem to make any sense. You copy data between two instances of some type, say, clone one one instance to obtain a new one. It is not related to "DLL" or not "DLL". What would "main program" mean? How can be not a "main program"?

"Transferring data to DLL" — same thing. A DLL is an executable module of some assembly (or it could be a native DLL). You do not copy data to an executable code, you copy data to data.

What is "deceleration of structure"? It means "slow a structure down", which does not seem to make any sense.

"Code... that fails"... What is "fails" (exception? something else? in what line?)?

"Converting structure to the object"... what is it? "Structure" means data type. "Object" means something existing in memory, an instance of some type. How one could convert a type to object? Well, you can instantiate a type, but is should not be a problem. Convert to an object of different type, perhaps a class? What type then?

And so on...

I would only note that finding a method by name is quite possible, but this is a bad idea, as such code is bad for maintenance. There are much better approaches based on reflection.

Sergey Alexandrovich Kryukov 24-Jul-13 0:51am    
And please pay attention for both of your answers and my comments to them. I saw them before: they are pure fake. Please don't do such things.

Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.

Please also keep in mind that posting non-answers as answer could cause some abuse reports. In the case of massive abuse, some members already lost their membership accounts, due to member's abuse reports.

Georg Kohler 24-Jul-13 1:22am    
You again ??? I'm getting confused - again ?? what's all this miles of talk about something that has nothing to do with my actual question ??
I really love this Blog and have been asking on it for 10 years ?? or for sure for a long long time .. but I really don't understand what all this "Political" talk is about.
OK I understand you know a lot lot more about programming then most of us do and I don't know the correct terms to be used so lets leave that aside...

I come here to get some advice on how to solve probably rather simple problems that I just don't understand ..

Now to the Question ... It's very very simple ...
I have data inside a structure that I need to copy over in one step to my DLL -It's that simple
I use Reflection (i hope that the correct term if not I'm really sorry) to load and communicate with my Dll.. now at the point where I try to convert the data back into a structure on the DLL side is where I get in trouble - The data is there (You can "see" it in the debugger) but it fails to convert the data back into my new structure.. That simple
Maybe I need to create a new sub that Instead of the Object is passing the Structure ?? Could it be that simple ?? for whatever reason I thought that when using Reflection the only data type allowed to be "transmitted" is an Object (again sorry for my bad programming English)
is that correct or that what I'm missing - I haven't tried that yet but I will in a bit

Maybe this helped solving some of the misunderstandings ? I sure hope so


Richard MacCutchan 24-Jul-13 3:33am    
You would need to upload your file to some file sharing web site, although most people are not happy to download such things.

:-( "nobody" had a suggestion ..
At this point I just put the values of the structure in a string - move them over into the DLL and then copy the values back into another structure (it's crap but it allows me to move on until I have more time to look into this)

Luckily the structure won't change much in the near future - just need to be careful to keep the correct orders ...but It works :-)

Like it or not - that's my crappy "solution" for now

Share this answer
I guess I messed up should Have clicked on solved it myself ..
Share this answer
I "think" I know what's going on here...

You have the structure defined in the DLL, you define another structure with the same members in another program and try to convert one to the other.

You can't do that and it will throw an exception. Even though the data types are the same structure and same members (and even the same name), you can't cast one to the other since they are different types. I don't know what the solution to this is in VB, but in C# I would use reflection to create the type from the DLL and use the dynamic keyword to interact with it.

You might be able to dive into the unmanaged world and try marshaling and unmarshaling to get them to match, but it makes for unsafe code.

If this isn't it then your question is very confusing, and even your example doesn't make a lot of sense out of context, I think you would have to show the full example to make sense.
Share this answer
Georg Kohler 25-Jul-13 10:26am    
I think you're right - I ended up with the same "conclusion" (not that I understand why or what's the actual reason behind it)
I realized it once I started using ByRef in the DLL sub
>Public Sub DLL_SUB(ByRef Par As Object)<
It ended up giving me a more detailed error Message saying Object [A] can not be copied into Object [B] because they don't have the same origin - with the full path to both structure definitions - both are identical as one is a copy by ref of the other ....
Anyway I gave up at that point ... the current solution of translating the structure into a string and back works just fine - all It needs now is a better way of creating the string in a loop to collect all items from the structure...(avoiding typos)
Thanks for looking into it for me :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900