HighTechTalks DotNet Forums  

String members of structure not allowed?

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss String members of structure not allowed? in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Ray
 
Posts: n/a

Default String members of structure not allowed? - 06-27-2007 , 11:52 AM






I have some existing vb6 code that I am moving to a .net dll for easy
access from both vb6 apps as well as vb.net apps. There are some
custom api calls that use structures that have strings in them. I
didn't think this would be a problem but I find that if I put a string
in a structure it is no longer usable through com interop in a vb6
application. Is this correct or am I doing something wrong.

..net example code:
Public Structure test_type
Public junk As String
Public hello As Short
End Structure

vb6 example code:
private sub command1_click()
dim test as mylib.test_type

test.junk = "12345"
test.hello = 1776
end sub

I get "Variable uses an automation type not supported in visual basic"
error as soon as I click the button. I set a break point on test.junk
= "12345" and it never makes it there.

Any help or guidance on this would be greatly appriciated.

Raymond


Reply With Quote
  #2  
Old   
SvenC
 
Posts: n/a

Default Re: String members of structure not allowed? - 06-27-2007 , 12:18 PM






Hi,

Quote:
I have some existing vb6 code that I am moving to a .net dll for easy
access from both vb6 apps as well as vb.net apps. There are some
custom api calls that use structures that have strings in them. I
didn't think this would be a problem but I find that if I put a string
in a structure it is no longer usable through com interop in a vb6
application. Is this correct or am I doing something wrong.

.net example code:
Public Structure test_type
Public junk As String
Public hello As Short
End Structure
In .Net structs are value types and can only consist of value type.
Make it a class and you can put ref types like String in there.

Quote:
vb6 example code:
private sub command1_click()
dim test as mylib.test_type

test.junk = "12345"
test.hello = 1776
end sub

I get "Variable uses an automation type not supported in visual basic"
error as soon as I click the button. I set a break point on test.junk
= "12345" and it never makes it there.
Structs should not have worked in VB6 as automation types, too. How is
mylib.test_type defined in VB6?

--
SvenC



Reply With Quote
  #3  
Old   
Ray
 
Posts: n/a

Default Re: String members of structure not allowed? - 06-27-2007 , 12:36 PM



On Jun 27, 11:18 am, "SvenC" <S... (AT) community (DOT) nospam> wrote:
Quote:
Hi,

I have some existing vb6 code that I am moving to a .net dll for easy
access from both vb6 apps as well as vb.net apps. There are some
custom api calls that use structures that have strings in them. I
didn't think this would be a problem but I find that if I put a string
in a structure it is no longer usable through com interop in a vb6
application. Is this correct or am I doing something wrong.

.net example code:
Public Structure test_type
Public junk As String
Public hello As Short
End Structure

In .Net structs are value types and can only consist of value type.
Make it a class and you can put ref types like String in there.

vb6 example code:
private sub command1_click()
dim test as mylib.test_type

test.junk = "12345"
test.hello = 1776
end sub

I get "Variable uses an automation type not supported in visual basic"
error as soon as I click the button. I set a break point on test.junk
= "12345" and it never makes it there.

Structs should not have worked in VB6 as automation types, too. How is
mylib.test_type defined in VB6?

--
SvenC
Thanks for the Reply.

The mylib is simply the dll name that the above .net code is located
in. The vb6 project has a reference to it in the references list.

I'll create a class and see if it works in place of the structure in
the api call.
Thanks,
Raymond



Reply With Quote
  #4  
Old   
Ben Voigt [C++ MVP]
 
Posts: n/a

Default Re: String members of structure not allowed? - 06-27-2007 , 11:55 PM




"SvenC" <SvenC (AT) community (DOT) nospam> wrote

Quote:
Hi,

I have some existing vb6 code that I am moving to a .net dll for easy
access from both vb6 apps as well as vb.net apps. There are some
custom api calls that use structures that have strings in them. I
didn't think this would be a problem but I find that if I put a string
in a structure it is no longer usable through com interop in a vb6
application. Is this correct or am I doing something wrong.

.net example code:
Public Structure test_type
Public junk As String
Public hello As Short
End Structure

In .Net structs are value types and can only consist of value type.
Make it a class and you can put ref types like String in there.
Total nonsense. A reference is itself just a value, and you certainly can
have references inside structs. The trouble is that there are many many
ways to pass a string through p/invoke, and you have to tell it which. I
think there's a MarshalAsAttribute that should come with good examples.

Quote:
vb6 example code:
private sub command1_click()
dim test as mylib.test_type

test.junk = "12345"
test.hello = 1776
end sub

I get "Variable uses an automation type not supported in visual basic"
error as soon as I click the button. I set a break point on test.junk
= "12345" and it never makes it there.

Structs should not have worked in VB6 as automation types, too. How is
mylib.test_type defined in VB6?

--
SvenC


Reply With Quote
  #5  
Old   
Ray
 
Posts: n/a

Default Re: String members of structure not allowed? - 06-28-2007 , 11:12 AM



On Jun 27, 10:55 pm, "Ben Voigt [C++ MVP]" <r... (AT) nospam (DOT) nospam> wrote:
Quote:
"SvenC" <S... (AT) community (DOT) nospam> wrote in message

news:CF59DE5D-0E9B-494D-A150-518EB5D490E1 (AT) microsoft (DOT) com...





Hi,

I have some existing vb6 code that I am moving to a .net dll for easy
access from both vb6 apps as well as vb.net apps. There are some
custom api calls that use structures that have strings in them. I
didn't think this would be a problem but I find that if I put a string
in a structure it is no longer usable through com interop in a vb6
application. Is this correct or am I doing something wrong.

.net example code:
Public Structure test_type
Public junk As String
Public hello As Short
End Structure

In .Net structs are value types and can only consist of value type.
Make it a class and you can put ref types like String in there.

Total nonsense. A reference is itself just a value, and you certainly can
have references inside structs. The trouble is that there are many many
ways to pass a string through p/invoke, and you have to tell it which. I
think there's a MarshalAsAttribute that should come with good examples.





vb6 example code:
private sub command1_click()
dim test as mylib.test_type

test.junk = "12345"
test.hello = 1776
end sub

I get "Variable uses an automation type not supported in visual basic"
error as soon as I click the button. I set a break point on test.junk
= "12345" and it never makes it there.

Structs should not have worked in VB6 as automation types, too. How is
mylib.test_type defined in VB6?

--
SvenC- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
I'm glad to hear that it should be possible. The VB6 to .net upgrade
wizard translated:
MakeCode As String * MakeCodeSize_int_gc
to:
<VBFixedString(MakeCodeSize_int_gc),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=MakeCodeSize_int_gc)> Public MakeCode() As Char

This is what generates the "Variable uses an automation type not
supported in visual basic" error. Because of your message here I tried
other options including the SafeArray instead of ByValArray and it
works. It isn't size limited like it is supposed to be though. It
should be restricted to MakecodeSize_int_gc. If you know the proper
MarshalAs type for a fixed length string I would appreciate knowing. I
will play with it in the mean time and see if I can figure it out.

Thanks for the response.
Raymond



Reply With Quote
  #6  
Old   
=?Utf-8?B?U3RldmVS?=
 
Posts: n/a

Default Re: String members of structure not allowed? - 07-09-2007 , 02:50 AM



I use the following attributes and it works OK when I call it from C++ and
another developer has managed to call it succesfully from VB so give this a
try.

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string Comment;
--
Steve


"Ray" wrote:

Quote:
On Jun 27, 10:55 pm, "Ben Voigt [C++ MVP]" <r... (AT) nospam (DOT) nospam> wrote:
"SvenC" <S... (AT) community (DOT) nospam> wrote in message

news:CF59DE5D-0E9B-494D-A150-518EB5D490E1 (AT) microsoft (DOT) com...





Hi,

I have some existing vb6 code that I am moving to a .net dll for easy
access from both vb6 apps as well as vb.net apps. There are some
custom api calls that use structures that have strings in them. I
didn't think this would be a problem but I find that if I put a string
in a structure it is no longer usable through com interop in a vb6
application. Is this correct or am I doing something wrong.

.net example code:
Public Structure test_type
Public junk As String
Public hello As Short
End Structure

In .Net structs are value types and can only consist of value type.
Make it a class and you can put ref types like String in there.

Total nonsense. A reference is itself just a value, and you certainly can
have references inside structs. The trouble is that there are many many
ways to pass a string through p/invoke, and you have to tell it which. I
think there's a MarshalAsAttribute that should come with good examples.





vb6 example code:
private sub command1_click()
dim test as mylib.test_type

test.junk = "12345"
test.hello = 1776
end sub

I get "Variable uses an automation type not supported in visual basic"
error as soon as I click the button. I set a break point on test.junk
= "12345" and it never makes it there.

Structs should not have worked in VB6 as automation types, too. How is
mylib.test_type defined in VB6?

--
SvenC- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

I'm glad to hear that it should be possible. The VB6 to .net upgrade
wizard translated:
MakeCode As String * MakeCodeSize_int_gc
to:
VBFixedString(MakeCodeSize_int_gc),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=MakeCodeSize_int_gc)> Public MakeCode() As Char

This is what generates the "Variable uses an automation type not
supported in visual basic" error. Because of your message here I tried
other options including the SafeArray instead of ByValArray and it
works. It isn't size limited like it is supposed to be though. It
should be restricted to MakecodeSize_int_gc. If you know the proper
MarshalAs type for a fixed length string I would appreciate knowing. I
will play with it in the mean time and see if I can figure it out.

Thanks for the response.
Raymond



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.