PDA

View Full Version : .net question


davestewart
06-18-2008, 12:35 AM
I would have posted in the .net sticky, but at 8 pages already it's not so useable...

I've been chatting with someone about using .net to provide base64 encoded strings.

dnClass = dotNetClass "System.Convert"
dnValue = (dotNetObject "System.String" "Dave")
dnClass.ToBase64String dnValue

dotNetClass:System.Convert
dotNetObject:System.String
-- Runtime error: No method found which matched argument list

I know next to nothing about .net, or how to lever it, so I don't really understand the error, or what I'm obviously doing wrong to not be able to access the method.

.[static]<System.String>ToBase64String inArray
.[static]<System.String>ToBase64String inArray options
.[static]<System.String>ToBase64String inArray offset length
.[static]<System.String>ToBase64String inArray offset length options



Looking forward to someone shedding some light on the basics for me,
Thanks,
Dave

magicm
06-18-2008, 01:14 AM
I've tried the same and found that it simply doesn't work. The ToBase64String method expects a byte array, which you cannot create using mxs. I wrote a dll with a couple of simple wrappers, including a method to convert strings to base64, but it's part of a larger project which I can't share. I can put just this functionality in a single dll if you want though, but the downside is that it adds an extra dependency to your script.

Here's how (I think) it should work:

(

dnEncoding = (dotNetClass "System.Text.Encoding").Unicode -- or UTF8 or ASCII or ..
dnConvert = dotNetClass "System.Convert"
dnValue = dotNetObject "System.String" "Dave"

-- Convert the string to an array of bytes.
Bytes = dnEncoding.GetBytes dnValue

-- Get the Base64 string using these bytes.
dnConvert.ToBase64String Bytes

-- Runtime exception ...
-- max automatically converts the byte array retrieved from the
-- GetBytes method to an array of integers, which dotNet is unable
-- to convert back to an array of bytes ...

)

Cheers,
Martijn

ZeBoxx2
06-18-2008, 01:22 AM
a byte array is just an array of, well, bytes; in this case stored as 8bit ints. No reason you shouldn't be able to use it...


dncConvert = dotNetClass "System.Convert"
dotNetClass:System.Convert

dnvStringIn = (dotNetObject "System.String" "Dave")
dotNetObject:System.String

dnoASCII = dotNetObject "System.Text.ASCIIEncoding"
dotNetObject:System.Text.ASCIIEncoding

dnvByteArrayIn = dnoASCII.getBytes dnvStringIn
#(68, 97, 118, 101)

dnvB64String = dncConvert.ToBase64String dnvByteArrayIn
"RGF2ZQ=="

dnvByteArrayOut = dncConvert.FromBase64String dnvB64String
#(68, 97, 118, 101)

dnoUTF8 = dotNetObject "System.Text.UTF8Encoding"
dotNetObject:System.Text.UTF8Encoding

dnvStringOut = dnoUTF8.getString dnvByteArrayOut
"Dave"



Whether that does what you need it to do, however...

davestewart
06-18-2008, 09:29 AM
Hey guys,

Thanks so much for the info. We're just chucking ideas around at the moment, but that's really helpful.

Richard, in particular, thanks for working through the sticky stuff. .NET seems quite rigourous / unforgiving so it's good to have an example.

With that in mind, how would one go about getting to know how to use .NET without feeling like it's bashing your head against a brick wall? Do I need to get into Visual Studio and C# type stuff first?

Cheers,
Dave

LoneRobot
06-18-2008, 11:12 AM
hi Dave,

i'd just go to microsoft and download the visual studio express edition. I went for the VB flavour, as i thought it would be a wee bit more straightforward than c#.
it's free and even without getting into it you will be able to see via the intellisense prompt how to declare the classes and what arguments and overloads pertain to each. It really will remove the guesswork about using .net within max. on a side note im frustrated as I am hoping to post some of my dotnet research soon, including usercontrols and a dotnet maxfile thumbnail class but am totally swamped at the mo. :argh:

ZeBoxx2
06-18-2008, 12:22 PM
in addition to grabbing express, I'd say find the nearest book store and pick up a copy of a beginner-level .NET book; there's plenty of resources online as well, of course, but books tend to have better flow :)

magicm
06-18-2008, 12:52 PM
So the ToBase64String method does work for you guys?
Weird.. here's what I get:

-- Runtime error: dotNet runtime exception: Cannot widen from source type to target type either because the source type is a not a primitive type or the conversion cannot be accomplished.

This is on max2008 32bit btw.

Martijn

ZeBoxx2
06-18-2008, 01:05 PM
So the ToBase64String method does work for you guys?
In 3ds max 2009 - sure!
( nothing noted in the help file topic "What's new in..." )

This is on max2008 32bit btw.
As some people frequently tell me... "who cares about older max versions?" ;)

Confirmed, for what it's worth (ditto 3ds Max 9) - so either they silently fixed something somewhere, or it was automagically fixed with the new compile.

Edit: I don't remember where this was linked from, but I remember I had browsed it before;
The following link should go to a post at The Area that deals with byte array handling in maxscript
http://area.autodesk.com/index.php/forums/viewthread/13286/


The code then* becomes something like:

dncConvert = dotNetClass "System.Convert"
dotNetClass:System.Convert
dnvStringIn = (dotNetObject "System.String" "Dave")
dotNetObject:System.String
dnoASCII = dotNetObject "System.Text.ASCIIEncoding"
dotNetObject:System.Text.ASCIIEncoding
dnvByteArrayIn = dnoASCII.getBytes dnvStringIn
#(68, 97, 118, 101)

-- ByteArrayTools
arry = dotnetobject "System.Byte[]" dnvByteArrayIn.count
dotNetObject:System.Byte[]
for i = 1 to dnvByteArrayIn.count do ( ByteArrayTools.SetValue arry (i - 1) dnvByteArrayIn[i] )
OK
-- /ByteArrayTools

dnvB64String = dncConvert.ToBase64String arry
"RGF2ZQ=="

dnvByteArrayOut = dncConvert.FromBase64String dnvB64String
#(68, 97, 118, 101)
dnoUTF8 = dotNetObject "System.Text.UTF8Encoding"
dotNetObject:System.Text.UTF8Encoding

-- ByteArrayTools
arry = dotnetobject "System.Byte[]" dnvByteArrayOut.count
dotNetObject:System.Byte[]
for i = 1 to dnvByteArrayOut.count do ( ByteArrayTools.SetValue arry (i - 1) dnvByteArrayOut[i] )
OK
-- /ByteArrayTools

dnvStringOut = dnoUTF8.getString arry
"Dave"

( tested in 3ds Max 9x32 )

Edit: Added link to The Area post
Edit2: Added sample code using the assembly created from The Area post
Edit3: * after evaluating the 'CreateAssembly' and 'CompileByteArrayTools' functions from The Area post.

magicm
06-18-2008, 01:17 PM
Thanks for checking. I'm glad I won't be needing that dll anymore :)

Edit: And thanks for posting that link as well Richard, useful stuff!

Cheers,
Martijn

JHN
06-18-2008, 01:47 PM
Also note on the .net sticky thread I posted a message from someone on the area who found a way to compile dotnet classes/objects in mxs runtime... which leads to "probably" never have to compile a dll again! The .cs file as string can be used.

-Johan

ZeBoxx2
06-18-2008, 02:25 PM
Edit: I don't remember where this was linked from, but I remember I had browsed it before

Also note on the .net sticky thread I posted a message from someone on the area who found a way to compile dotnet classes/objects in mxs runtime...
Aha, that was where :D had browsed the non-sticky threads to find the reference and came up empty-handed :)

CGTalk Moderation
06-18-2008, 02:26 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.