代码之家  ›  专栏  ›  技术社区  ›  Amy

c#与ghostscript互操作

  •  0
  • Amy  · 技术社区  · 14 年前

    我正在尝试访问一些Ghostscript函数,例如:

    [DllImport(@"C:\Program Files\GPLGS\gsdll32.dll", EntryPoint = "gsapi_revision")]
    public static extern int Foo(gsapi_revision_t x, int len);
    
    public struct gsapi_revision_t
    {
        [MarshalAs(UnmanagedType.LPTStr)]
        string product;
    
        [MarshalAs(UnmanagedType.LPTStr)]
        string copyright;
    
    
        long revision;
        long revisiondate;
    }
    
    public static void Main()
    {
        gsapi_revision_t foo = new gsapi_revision_t();
        Foo(foo, Marshal.SizeOf(foo));
    

    typedef struct gsapi_revision_s {
        const char *product;
        const char *copyright;
        long revision;
        long revisiondate;
    } gsapi_revision_t;
    
    GSDLLEXPORT int GSDLLAPI 
    gsapi_revision(gsapi_revision_t *pr, int len);
    

    public struct gsapi_revision_t
    {
        IntPtr product;
        IntPtr copyright;
    
        long revision;
        long revisiondate;
    }
    
    public static void Main()
    {
        gsapi_revision_t foo = new gsapi_revision_t();
    
        IntPtr x = Marshal.AllocHGlobal(20);
        for (int i = 0; i < 20; i++)
            Marshal.WriteInt32(x, i, 0);
    
        int result = Foo(x, 20);
        IntPtr productNamePtr = Marshal.ReadIntPtr(x);
        IntPtr copyrightPtr = Marshal.ReadIntPtr(x, 4);
        long revision = Marshal.ReadInt64(x, 8);
        long revisionDate = Marshal.ReadInt64(x, 12);
    
        byte[] dest = new byte[1000];
        Marshal.Copy(productNamePtr, dest, 0, 1000);
    
    
        string name = Read(productNamePtr);
        string copyright = Read(copyrightPtr);
    }
    
        public static string Read(IntPtr p)
        {
            List<byte> bits = new List<byte>();
            int i = 0;
    
            while (true)
            {
                byte b = Marshal.ReadByte(new IntPtr(p.ToInt64() + i));
                if (b == 0)
                    break;
    
                bits.Add(b);
                i++;
            }
    
            return Encoding.ASCII.GetString(bits.ToArray());
        }
    

    那我在做什么错事?

    1 回复  |  直到 14 年前
        1
  •  1
  •   GBegen    14 年前

    UnmanagedType.LPTStr 依赖于平台(ANSI在Win98上,Unicode在NT/XP上)。您的C++结构使用 char * UnmanagedType.LPStr 相反。

    long C中是64位,而C++中的长是32位。你可能想用 int