|
Thanks for the reply Heath.
I have tried to call "DefineDynamicAssembly" on a secondary AppDomain but I always get a SerializationException. Basically says AssemblyBuilder is not marked as Serializable. I'm guessing that might have something to do with the AppDomainSetup of my new AppDomain. Would could solve that problem?
As for the Load function mentioned above, I dont believe I could use that because I need to generate the compiled code with an AssemblyBuilder, ModuleBuilder, MethodBuilder, etc, and I believe DefineDynamicAssembly is the only way to initialize the AssemblyBuilder to compile OpCodes.
|
|
|
|
|
That's to be expected, based on the quote I posted. The reflection-related classes are not MarshalByRefObject implementation and are not serializable so they cannot be remoted across AppDomain boundaries. In the case of Assembly.Load (from where that quote was taken) an Assembly could be returned, but it will cause the assembly you load to be re-loaded into the current AppDomain . In your case, since you're defining a dynamic assembly there is no assembly to return.
As the exception states, too, AssemblyBuilder is not serializable, just like an Assembly reference you're trying to create.
In short, this isn't really possible doing it like you're trying to do.
That's not to say there isn't a way, however. Create an assembly factory class that is remotable (extends MarshalByRefObject and is attributed with SerializableAttribute and load that into a new AppDomain , then make remoted calls on it in order to create a dynamic assembly. Since the factory exists in the new AppDomain , when it calls DefineDynamicAssembly in will be in the scope of the new AppDomain .
Read Accessing Objects in Other Application Domains Using .NET Remoting[^] for more abour remoting. .NET provides a remoting channel for specifically communicating between AppDomain s in the same process, so you do not want to specify a TcpChannel or HttpChannel like you'd use to communicate between two disparate processes, potentially on two different machines.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Ok, thanks a lot for the help Heath. That method should work
|
|
|
|
|
Hi,
I've got a "Manager" class which, whitin it's own declaration, has another class declared: "ItemCollection". I'd like to be able to instanciate an ItemCollection class inside the Manager class and make it from outside accessible as a return value for a function. But I don't want that any code outside the Manager class can instanciate the ItemCollection.
How would I go about this? Thanks in advance.
/matthias
I love deadlines. I like the whooshing sound they make as they fly by. [Douglas Adams]
|
|
|
|
|
|
You could also provide "outside" access to this ItemCollection class via a public interface (implemented by ItemCollection) which you pass back to a caller from your function
Phil Harding. myBlog [^] | mySite [^]
|
|
|
|
|
Hello,
I need to access a frame (filling inputs, reading data, etc.) in an activeX webbrowser.
ie: an html page embeds 2 frames, one on the same domain (no probs using this frame), and the other that refers a page on another domain.
The problem is that IE component doesnt allow access to a frame (read and more) that is not in the same domain as the parent window.
A friend success to access this kind of frame using Delphi, but I wonder if there is a way to do the same using C#.
Regards.
|
|
|
|
|
Are you using JavaScript/JScript or VBScript to do this in a web page? Cross-site scripting is there for a reason, and this question wouldn't have anything to do with C# or .NET, so please refer your question to the Web Development[^] forum.
C# won't simply solve your problem if you're talking about a client scenario only. An ActiveX control or embedded Windows Forms control could possibly access the other frame through a few methods, but if you try to use the MSHTML DOM (document object model) to do this then no language will help since those calls - the very same calls that scripts automate - will be blocked by the implementation, which is implemented by mshtml.dll (the MSHTML DOM).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I am not using any scripts in the web page. I am just trying to fill some form inputs using MSHTML ActiveX, but the MSHTML component doesnt let me access a frame outside the domain of the parent web page.
So I'm looking for another way to access the frames objects. Any ideas ?
|
|
|
|
|
Could you please provide more information about what you're doing? Are you interoperating with the WebBrowser control in a Windows Forms application? Please be specific.
As I mentioned in my previous post, if you use the methods of the MSHTML DOM objects - no matter if done through JScript, VBScript, C#, C++, etc. - they will block against cross-site scripting. This is intentional and is implemented in the MSHTML DOM itself, so there's no way around it using the MSHTML DOM methods.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
At this moment using MSHTML works perfectly (in the same domain), I know that cross-frame scripting is restricted using MSHTML.
I am using the ActiveX axWebBrowser object to embed a webbrowser in my app. But in some case, web pages embeds frame out of the parent domain. So I am stuck if I want (for exemple) to login on external website, due to cross-frame scripting restrictions.
So I wonder if there is a way to do this in C#... My friend that did the thing in Delphi seems to get the main webbrowser document id in an OleContainer then enumerate objects in this container, after he cast back the OleObject to a IHTMLWindow and access the frame without any restrictions.
Here is the link where he find the solution in Delphi (unfortunaly he doesnt know what he does, but use the code as it):
http://www.swissdelphicenter.ch/torry/showcode.php?id=2054
|
|
|
|
|
Okay, not that I know what you're trying to do and how you're using the WebBrowser control it makes more sense.
You can do the same thing, but those interfaces your friends is using aren't defined in the .NET Framework class libraries. You have to either create an interop assembly, or write your own interfaces with the right method signatures and GUIDs (using the GuidAttribute ) and the ComImportAttribute that match up to the native interfaces like IOleObject .
This can be a daunting task, however. First, please read Exposing COM Components to the .NET Framework[^]. It's imperative that you understand COM interoperability in .NET. You'll also need to these interfaces in managed code so that the CLR will marshal calls from your managed code to native code (the COM implementation).
Fortunately, there's a relatively easy way since defining these interfaces by hand would take a long time and require quite a bit of experience with marshaling the various data structures and interfaces required.
Make an IDL file that forward-declares the interfaces you need, run the IDL file through midl.exe, then run the resulting typelib (.tlb file) through tlbimp.exe. midl.exe is the Microsoft IDL compiler that ships with the Platform SDK and Visual Studio .NET, and tlbimp.exe is the Type Library Importer that ships with the .NET Framework SDK, which is installed by default with Visual Studio .NET as well.
For an example of how this is done, read Using MSHTML Advanced Hosting Interfaces[^] where Nikhil uses the technique above to creating an interop assembly (that contains interop interfaces and enumerations and structure required to call them) for the MSHTML and WebBrowser hosting interfaces.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Okay
Thx for pointing me to some nice things to read.
Regards.
|
|
|
|
|
Can you help me about project in the image processing
(how can you discovery disease in cell boold employmend computer )
image reading next recognize on disease.
plz help me......
|
|
|
|
|
|
I need to center a text in the promt how can I do that??
|
|
|
|
|
What prompt? The console prompt? Please be specific.
For now I'll assume you're referring to the console window, since your last question was about the console text colors. You can P/Invoke GetConsoleScreenBufferInfo as I did in my previous example[^], but change the 1st, 2nd, and 5th fields to COORD and define a COORD structure like so:
[StructLayout(LayoutKind.Sequential)]
struct COORD
{
public short x;
public short y;
} After calling GetConsoleScreenBufferInfo in the same manner I did previously, you can get csbi.dwSize.x for the width and pad your string with spaces accordingly to center it.
Note that the Compact Framework (.NET CF) doesn't marshal nested structures, so if you have to support .NET CF you can you can leave the CONSOLE_SCREEN_BUFFER_INFO struct as a defined it and add a method like so to get a Size with the necessary coordinates (don't forget to add System.Drawing to your using clauses to use Size without fully-qualifying it):
[CLSCompliant(false)]
static Size GetSize(uint coord)
{
return new Size(
(int)(coord & 0xffff0000 >> 16),
(int)(coord & 0x0000ffff));
} After resetting the console text attributes in my previous example, I extended my source to add the following at the end of Main :
Size s = GetSize(csbi.dwSize);
string text = "This is a centered text message.";
int lpad = (s.Width - text.Length) / 2;
text = text.PadLeft(lpad + text.Length);
Console.WriteLine(text);
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Well its a C++ MFC program
|
|
|
|
|
Why are you asking in the C# forum, then? Please use the appropriately forums.
Since GetConsoleScreenBufferInfo is a native function, you can call it without P/Invoking, making it even easier. Read the documentation[^] for GetConsoleScreenBufferInfo in the MSDN Library.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Can I access a cookie set by a browser through a winform?
|
|
|
|
|
You can P/Invoke InternetGetCookie like in the following example:
using System;
using System.Runtime.InteropServices;
class CookieSample
{
[DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern bool InternetGetCookie(string lpszUrlName,
string lpszCookieName, [Out] string lpszCookieData,
out uint lpdwSize);
const int ERROR_SUCCESS = 0;
const int ERROR_INSUFFICIENT_BUFFER = 122;
const int ERROR_NO_MORE_ITEMS = 259;
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.Error.WriteLine("Error: please specify a URL.");
Environment.Exit(1);
}
try
{
string data = GetCookie(args[0]);
if (data != null)
Console.WriteLine("Cookie data: " + data);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
Environment.Exit(1);
}
}
static string GetCookie(string url)
{
int error;
uint dwSize = 0;
if (InternetGetCookie(url, null, null, out dwSize))
{
error = Marshal.GetLastWin32Error();
if (error == ERROR_SUCCESS)
{
string data = new string('\0', (int)dwSize);
if (InternetGetCookie(url, null, data, out dwSize))
{
return data;
}
}
}
error = Marshal.GetLastWin32Error();
if (error == ERROR_NO_MORE_ITEMS)
{
Console.Error.WriteLine("Error: no cookie for URL or parents.");
return null;
}
throw new Exception("Error: unexpected failure.");
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Brilliant! Thanks alot.
Do you know how to get the cookies from other browsers (firefox, opera) as this only seems to work with IE.
|
|
|
|
|
You should read the documentation for those applications and explore how they store and allow access to cookies. InternetGetCookie is a WinInet function, an API on which parts of Internet Explorer are built.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello,
How can I use diffrent color on the text or background in a consol program?. And is it possible to show a *.bmp picture?
|
|
|
|
|
In .NET 2.0 the Console class will define new methods for setting text color, but for now you'd have to P/Invoke SetConsoleTextAttribute , as well as GetConsoleScreenBufferInfo .
using System;
using System.Runtime.InteropServices;
class ConsoleSample
{
[DllImport("kernel32.dll")]
static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput,
TextAttributes wAttributes);
[Flags]
enum TextAttributes : short
{
None = 0,
FOREGROUND_BLUE = 1,
FOREGROUND_GREEN = 2,
FOREGROUND_RED = 4,
FOREGROUND_INTENSITY = 8,
BACKGROUND_BLUE = 16,
BACKGROUND_GREEN = 32,
BACKGROUND_RED = 64,
BACKGROUND_INTENSITY = 128
}
[DllImport("kernel32.dll")]
static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput,
out CONSOLE_SCREEN_BUFFER_INFO csbi);
struct CONSOLE_SCREEN_BUFFER_INFO
{
public uint dwSize;
public uint dwCursorPosition;
public TextAttributes wAttributes;
public ulong srWindow;
public uint dwMaximumWindowSize;
}
[DllImport("kernel32.dll")]
static extern IntPtr GetStdHandle(StdHandle nStdHandle);
enum StdHandle : uint
{
STD_INPUT_HANDLE = unchecked((uint)-10),
STD_OUTPUT_HANDLE = unchecked((uint)-11),
STD_ERROR_HANDLE = unchecked((uint)-12)
}
static void Main()
{
IntPtr hStdErr = GetStdHandle(StdHandle.STD_ERROR_HANDLE);
TextAttributes attrs = TextAttributes.None;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hStdErr, out csbi))
{
Console.Error.WriteLine("Error: could not get current colors.");
Environment.Exit(1);
}
else
attrs = csbi.wAttributes;
if (!SetConsoleTextAttribute(hStdErr,
TextAttributes.FOREGROUND_RED | TextAttributes.FOREGROUND_INTENSITY))
{
Console.Error.WriteLine("Error: could not set current colors.");
Environment.Exit(1);
}
Console.Error.WriteLine("Sample error: this is a test error in red.");
SetConsoleTextAttribute(hStdErr, attrs);
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|