ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Mauricio Rojas Blog

January 2009 - Posts

  • Debug XBAP using WinForms Host

    Recently I had to deal with targeting an XBAP application that had some Windows Forms controls.

    The problem is that those controls can only be used in a trusted environment. If you try to debug an XBAP with some Windows Forms Controls you will get an exception like:

    Message: Cannot create instance of 'Page1' defined in assembly 'XBAPWithWinForms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation.  Error in markup file 'Page1.xaml' Line 1 Position 7.

    It took me a while to found a solution, and it was thru Scott Landford Blog that I found a way around.

    In short what he recommends to do is:

    Change your settings to:

    Start Action->Start external program = %windir%system32\PresentationHost.exe

      In my case (and the case of most people that is: c:\windows\system32\PresentationHost.exe)


    Start Options->Command line arguments = -debug "c:\projects\myproject\bin\debug\MyProject.xbap" -debugSecurityZoneUrl "http://localhost:2022"

    Copy the value from the Start URL after the –debug argument

    Very import for using WinForms components you must run in FULL TRUST

    fullTrust

     

    Here is some XBAP code using a WinForms WebBrowser. They designer does not like it a lot but it works:

    XBAPWithWinforms.zip

  • Welcome back Basic!!

    My first language ever was GW-Basic. I was a teenager doing in summer work at a company that made polls about hair products and things like that. At that time I didn’t had a computer. They were tooooo expensive.

    And I found a manual for GW-Basic. I stole it, to make a copy, my boss found out and he fired me :(. But then I had the power!! jeje. With my cousin Esteban we started out first programs. And we got our first sale :) :) :). It was the typical POS software for a small video rental  company.

    Ahhh, and that was just the beginning.

    So, I really appreciate basic, with all its strengths and weaknesses. I do most of my development now in C#, I think due to my C++, java years, and know I am more used to its syntax, and I really prefer C#. But I consider Basic a real treasure.

    Well recently while digging in the Microsoft Dev Labs I discovered SmallBasic. I was just browsing for some new stuff and I am glad to have found this jewel.

    Artinsoft (the company I work for) is a company that has devoted a lot of time in the research of automatic software migration to other platforms.Not for nothing one of our flapship products is Visual Basic Upgrade Companion, so basic in any form is always a delight for me. (I will soon post a brief article about bringing VBScript back to life in VB.NET just wait and see)

    Ok. Back to SmallBasic.

     

    First of all. The interface is great!!!

    image IDE

    It has a cool intellisense guide and you can even extend it :)

     

    And they even resurrected LOGO!

    image

    I just hope to write a cool Turtle Graphics routine soon!

    :)

  • The new way for Installing on the GAC

    A long long time ago, I faced the problem of installing an assembly on the GAC from code. And I even posted the code in my blog

     http://blogs.artinsoft.net/mrojas/archive/2008/04/09/install-assembly-gac-with-c.aspx

     

    Recently Jose Cornejo wrote me on my blog because the code was not working, and he finally showed me the right way to do it. It was sooo easy.

    His solution was:

    1. Add a reference to System.EnterpriseServices.dll to your project

    2. Write code like the following:

                System.EnterpriseServices.Internal.Publish p = new Publish();
                p.GacInstall(@"C:\ClassLibrary1.dll");
                p.GacRemove(@"C:\ClassLibrary1.dll");

     

    Thanks a lot Jose for your help.

  • Unofficial VarPtr, StrPtr, and ObjPtr migration to C#

    These interesting functions have a long history, since the BASIC language, QuickBasic
    and earlier versions of Visual Basic.  There isn’t much documentation on them but you
    can look at very good reference as the one published by Matthew Curland
    titled Unofficial Documentation for VarPtr, StrPtr, and ObjPtr

    Well enough history, now let’s get back to the the migration part. If you’re reading this
    post it might be, because you have VarPtr, StrPtr or ObjPtr calls in your code and you
    want to move those calls to C#.

    Well we have good and bad news.

    Bad news are that the .NET world is a lot different than VB. Remember that your code
    is running in the managed sandbox, and to get the address of variable you are probably
    dealing with unmanaged memory, so some things might not work.

    Good news are that I am one of those that believe that there are no imposibles,
    it’s just a matter of the cost of developing the solution :)

    Let’s see at some alternatives:

    VarPtr can be use to get pointers to vaiables. This can be solved using Unsafe code:

    VB6

      Dim l As Long
      Debug.Print VarPtr(l)

    C#

        class Program
        {
            [DllImport(@"DllTest1.Dll")]
            static extern void Foo(IntPtr p);
            static void Main(string[] args)
            {
                int l;
                unsafe
                {
                    int* pointerToL = &l;
                    Foo((IntPtr)pointerToL);
                    // Print the address stored in pointerToL:
                    System.Console.WriteLine(
    "The address stored in pointerToL: {0}", (int)pointerToL); } } }
    And The implementation for Foo is a C function like:
    extern "C" 
    {
    __declspec(dllexport)  void Foo(int* data)
    {
        *data = 100;
    }
    }
    Unsafe code like this will work for VarPtr cases where you have primitive types, 
    like int, short, char variables.
    Structures are more tricky and you will require pinning the memory to avoid some 
    other problems. But in general doing Interop with structures is very tricky and
    I will publish another post about this.
    Unsafe code must be enabled at the assembly level and the assembly might need to 
    be signed.
    StrPtr is very similar to VarPtr but it mostly to provide efficient marshalling 
    to Unicode functions. In most of cases like:
      Declare Sub MyUnicodeCall Lib "MyUnicodeDll.Dll" _
          (ByVal pStr As Long)
    
       Sub MakeCall(MyStr As String)
          MyUnicodeCall StrPtr(MyStr)
       End Sub
    The StrPtr declaration is no longer needed because the .NET Interop mechanism can 
    handle most of this marshaling automatically.
    ObjPtr is the most tricky of all because it can be used in COM scenarios to get 
    pointers to Interfaces implemented by a class.
    In .NET scenarios this will involve only using classes that are exposed by COM. 
    I have use code as the following
    for some of those cases:
                Object myComObject = null;
                //..init code
                IntPtr pIUnknown = Marshal.GetIUnknownForObject(myComObject);
                IntPtr pIDesiredInterface = IntPtr.Zero;
                Guid guidToDesiredInterface = new Guid("XXXXXXXX-XXXX-XXXX");
                Marshal.QueryInterface(pIUnknown, 
    ref guidToDesiredInterface,out pIDesiredInterface);

    As always there are exceptions to the rule. These are just some general solutions. 
    Directly accessing the memory is really something not desirable in a .NET application and in most
    cases you should remove that code for something else, but if you can’t I hope these examples
    guide you in this process.

This Blog

Syndication

Powered by Community Server (Non-Commercial Edition), by Telligent Systems