Results 1 to 5 of 5

Thread: [RESOLVED] How to use unsafe C# function in VB?

  1. #1

    Thread Starter
    Hyperactive Member Ivenesco's Avatar
    Join Date
    Sep 2007
    Location
    Poland, Lublin
    Posts
    325

    Resolved [RESOLVED] How to use unsafe C# function in VB?

    Hello,
    I've written C# class library and included it to my VB project. Everything works ok, unless I can't use my unsafe function...
    It's really important to have this unsafe function, through I wrote it to increase bitmap processing speed.
    So, how can I use it? When I try to type its name, IDE tells me, that it isn't member of that class.
    "Only two things are infinite; the universe and human stupidity, and I'm not sure about the former."
    Albert Einstein

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: How to use unsafe C# function in VB?

    So you wrote this DLL in C#, and you are referencing the C# compiled DLL from your VB app right?

    What does the code in your VB app look like when you try to access the function, and what does the class declaration and function declaration look like in the C# class?

  3. #3

    Thread Starter
    Hyperactive Member Ivenesco's Avatar
    Join Date
    Sep 2007
    Location
    Poland, Lublin
    Posts
    325

    Re: How to use unsafe C# function in VB?

    It's only test, to lern it, because I've never tried it before ;-)

    Whole C# project:
    Code:
    using System;
    using System.Collections.Generic;
    
    namespace CsGraphics
    {
        public class TransformBitmap
        {
    
            unsafe public static void RevertColours(byte* poczatek, int x)
            {
                for (int xx = 0; xx < x; xx++)
                {
                    *poczatek = (byte)(*poczatek ^ 255);
                    poczatek += 1;
                    *poczatek = (byte)(*poczatek ^ 255);
                    poczatek += 1;
                    *poczatek = (byte)(*poczatek ^ 255);
                    poczatek += 1;
                }
            }
        }
        public class TransformBitmap2
        {
            public static void LOL(int x)
            {
                x = 10;
            }
        }
    }
    "TransformBitmap2" and "LOL" is written to check, if it works anyway. And yes, my application see this function. But "RevertColours" not...
    "Only two things are infinite; the universe and human stupidity, and I'm not sure about the former."
    Albert Einstein

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,020

    Re: How to use unsafe C# function in VB?

    That might be because, as you probably know, VB does not support pointers. What I think you should do is make the DLL take an IntPtr instead, as both C# and VB can handle that datatype. You can then take the address from the IntPtr and cast it to a byte pointer inside the RevertColours method.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Hyperactive Member Ivenesco's Avatar
    Join Date
    Sep 2007
    Location
    Poland, Lublin
    Posts
    325

    Re: How to use unsafe C# function in VB?

    Aaaaargh!
    Right! So stupid mistake :-P I don't use C# very often, so I forgot about that, I'm trying to pass C# pointer instead of address to array. Ok, I know the way with IntPtr.
    Thanks ;-)
    "Only two things are infinite; the universe and human stupidity, and I'm not sure about the former."
    Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width