View Full Version : dotnet and byte arrays
Bennels 12-13-2006, 12:41 AM Hello,
I'm trying to write to a webrequest stream (to POST data), and I cannot get a byte array which is compatible. The max array which is returned on converting to bytes isn't working. So I've come up with this :
clearlistener()
str = "hello"
encoding = dotNetObject "System.Text.UTF8Encoding"
strBytes = encoding.GetBytes str
bytes = dotNetObject "System.Byte[]" strBytes.count
for i in 1 to strBytes.count do
(
byte = dotNetObject "System.Byte" strBytes
int = dotNetObject "System.Int32" (i-1)
byteArray.SetValue byte int
)
However, this doesn't get past the "byteArray.SetValue byte int" is keeps giving me the following error:
-- 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.
If anyone has any ideas on how to get this working, I would love to know...
Thanks
Ben
|
|
ypuech
12-13-2006, 07:01 AM
Hi Ben,
I've tried to get this working but it seems that the "byte" value is auto converted to Int32 by MAXScript :D.
There's several problem with dotNet in MAXScript in this kind of case. It's the same problem when we want to build an object array : object[].
ypuech
12-13-2006, 09:30 AM
So, I think that the best solution is to create a .NET class library (C#/VB.NET) that does the job for you.
In MAXScript you can pass it the string you want to post as a web request and in C#/VB.NET the encoding stuff is done.
Bennels
12-13-2006, 11:03 AM
Thanks ypuech, I'll look into this, as it's has been a long few days trouble shooting this problem.
I went to your website, seems you have some reading material on this very process. :)
I'll let you know how I go.
Thanks!
ypuech
12-13-2006, 11:41 AM
Hi Ben,
Yes I've written the article Using a .Net 2.0 Class Library in MASXcript (http://ypuechweb.free.fr/Files/dotNetClassLibraryHello.zip).
I think it will help some scripters that do not know advanced/windows programming.
Yannick,
thank you.
I still haven't installed my max 9, but after reading your pdf I am eager for it ...
Georg
Bennels
12-14-2006, 12:12 AM
Ok, so I've installed MS Visual C# Express and I've built an assembly. However, I can't seem to get the security web permissions working... And there seems to be little help available on the topic.
Here's what I have so far. It throws an exception when you try to connect()
#region Referenced Assemblies
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Permissions;
#endregion // Referenced Assemblies
// [assembly: WebPermissionAttribute(SecurityAction.RequestMinimum, Unrestricted = true)] // This doesn't work either.
// Usage in maxscript:
// request = dotNetObject "Wrangler.webrequest"
// request.url = "http://10.1.1.3"
// request.postdata = "project_id=hello"
// print (request.connect())
namespace wrangler
{
public class webrequest
{
private string url;
private string postdata;
public string Url { get { return url; } set { url = value; } }
public string Postdata { get { return postdata; } set { postdata = value; } }
#region Methods
public string connect()
{
//try
//{
// Create a WebPermission.instance.
WebPermission myWebPermission = new WebPermission(NetworkAccess.Connect, "http://10.1.1.3");
myWebPermission.AddPermission(NetworkAccess.Accept, "http://10.1.1.3");
myWebPermission.Demand();
// Everything after this should have permission to access http://10.1.1.3
// Create a 'WebRequest' object with the specified url.
WebRequest myWebRequest = WebRequest.Create("http://10.1.1.3");
UTF8Encoding utf8 = new UTF8Encoding();
// Encode the string.
Byte[] postBuffer = utf8.GetBytes(this.postdata);
// Set properties of the WebRequest.
myWebRequest.Method = "POST";
myWebRequest.ContentType = "application/x-www-form-urlencoded";
myWebRequest.ContentLength = postBuffer.Length;
myWebRequest.Timeout = 5000; // 5 seconds
Stream newStream = myWebRequest.GetRequestStream();
newStream.Write(postBuffer, 0, postBuffer.Length);
// Close the Stream object.
newStream.Close();
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream responseStream = myWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(responseStream, encode);
String htmlString = readStream.ReadToEnd();
readStream.Close();
return htmlString;
//}
//catch
//{
// return "No access";
//}
}
#endregion // Methods
}
}
Any help would be appeciated.
ypuech
12-14-2006, 07:01 AM
Hi Ben,
The problem in MAXScript is that you cannot debug the class library. So, the better solution is to test/debug it in C#; to view where the system throws an exception in connect().
I'm not very experienced in web development with .NET framework; you can find many .NET samples at http://www.codeproject.com/
There's many others interesting websites/communities covering .NET.
It's better to use the properties :
request.url = "http://10.1.1.3" -> request.Url
request.postdata = "project_id=hello" -> request.Postdata
MAXScript allows the access of private data ?!
Bennels
12-14-2006, 08:41 PM
Hi ypuech,
I've got it all sussed now... the permission problem was coming down to the location of the dll file, which was on a network drive, while it needed to be a sub dir of the 3dsmax root.
So now I can start learning some more dotnet, it really is quite powerful. Particularly in addition to maxscript.
Thanks for your help.
CGTalk Moderation
12-14-2006, 08:41 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.