标签: marshalling

C#中的StringBuilder编组问题

我试图使用C++ DLL在C#中编辑我的StringBuilder对象.我的C++代码如下所示:

extern "C" __declspec(dllexport) void __stdcall PrintHead(char* myString)
{
   myString = "testIsOkey";
}
Run Code Online (Sandbox Code Playgroud)

我的C#代码是:

[DllImport("StringEdit.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = false)]

public static extern void PrintHead([MarshalAs(UnmanagedType.LPStr)] StringBuilder stringBuilder);

private void button1_Click(object sender, EventArgs e)
{
   StringBuilder stringBuilder = new StringBuilder("123456");
   PrintHead(stringBuilder);
}
Run Code Online (Sandbox Code Playgroud)

在调用PrintHead之后,我希望看到stringBuilder对象的值从"123456"更改为"testIsOkey",但它不会更改.我无法弄清楚我在哪里弄错了.

谢谢你的帮助.

c# c++ stringbuilder marshalling

0
推荐指数
1
解决办法
3300
查看次数

在parcel中查找binder对象

如何在android包中找到binder对象?我想编组一个有位图的包裹,但我得到了运行时异常,说包裹有绑定对象.

android marshalling parcel android-binder

0
推荐指数
1
解决办法
2395
查看次数

在C#,封送处理类型中传递IntPtr指针后,在非托管C ++代码中分配数组

我正在尝试实现一些涉及托管C#和非托管C ++代码之间的封送处理数组的项目。我遇到了一个问题,我在网上找到的所有解决方案似乎都无效。我对此表示感谢。

我没有提供完整的代码,而是显示了问题的非常简化的部分。尽管它看起来很大,但非常简单-只是概念上的。只是想提供尽可能多的全貌。

C ++部分:

对象

class cObject
{
public:
    //...constructor, destructor...
    int Method_Known_Size(double* array, int size);
    int Method_Unknown_Size(double* array);
    ...
    void FreeArray(double* p);
}
Run Code Online (Sandbox Code Playgroud)

对象.cpp

int Method_Known_Size(double* array, int size)
{
    //modify array somehow..
    for(int i=0; i<size; i++) array[i] = i;

}

int method_Unknown_Size(double* array)
{
    int size = 9;
    array = new double[size];
    for(int i=0; i<size; i++) array[i] = i;
}
Run Code Online (Sandbox Code Playgroud)

(跳过Caller.h) Caller.cpp

//...callers for constructor, destructor, for releasing unmanaged memory...
extern "C" int __stdcall Run_Known_Size(cObject* pObject, double* …
Run Code Online (Sandbox Code Playgroud)

c# c++ unmanaged managed marshalling

0
推荐指数
1
解决办法
5287
查看次数

无法从c#调用用c ++编写的dll函数

我需要从C#console app调用一个用c ++编写的dll函数.我编写了编组并在C#中编写了相同的数据类型.但无论如何我得到错误.问题出在哪儿?

这是dll函数的代码

extern "C" {
SAMPLENATIVEDLL_API int GetCardInfoEx(INT64 Card, DWORD Restaurant, DWORD UnitNo, TCardInfoEx* Info, char* InpBuf, DWORD InpLen, WORD InpKind, char* OutBuf, DWORD OutLen, WORD OutKind) {
    int res = getCardInfoEx(Card, Info, UnitNo);
    return res;
}
Run Code Online (Sandbox Code Playgroud)

}

这是TCardInfoEx结构代码

#pragma pack(1)

typedef struct TCardInfoEx {

    WORD    Size;               
    BYTE    Deleted;            
    BYTE    Seize;              
    BYTE    StopDate;           
    BYTE    Holy;               
    BYTE    Manager;            
    BYTE    Blocked;            
    CHAR    WhyLock[256];       

    CHAR    Holder[40];         
    INT64   UserID;             
    DWORD   CardAccountNumber;  
    DWORD   TypeOfDefaulter;    
    WORD    Bonus;              
    WORD    Discount;           

    INT64   Summa;              

    INT64   AvailableSumma;     

    INT64 …
Run Code Online (Sandbox Code Playgroud)

c# c++ dll pinvoke marshalling

0
推荐指数
1
解决办法
251
查看次数

从c#应用程序访问Outlook的问题

我正在编写一个控制台应用程序,它检查Outlook邮箱的内容,以便将特定电子邮件的内容读入数据库.

无论Outlook是否开放,此应用程序在Visual Studio中都能正常运行.

如果我构建应用程序并从exe运行它只能在Outlook打开时工作,这不是一个真正的问题.

但是,我需要从计划任务运行它,因为它必须每隔几分钟运行一次.这根本不起作用.

我正在使用以下代码:

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("OUTLOOK");

int collCount = processes.Length;

if (collCount != 0)
{
    OutlookApp = Marshal.GetActiveObject("Outlook.Application") as Application;  
}
else
{
    OutlookApp = new Application();
} 
Run Code Online (Sandbox Code Playgroud)

我得到的错误信息是:

System.Runtime.InteropServices.COMException(0x800401E3)::操作不可用(0x800401E3(MK_E_UNAVAILABLE)从HRESULT异常)
在System.Runtime.InteropServices在System.Runtime.InteropServices.Marshal.GetActiveObject(GUID&rclsid,IntPtr的保留,对象&ppunk). ImportCruiseEmails.Program.Main()中的Marshal.GetActiveObject(String progID)

在线上 :

Marshal.GetActiveObject("Outlook.Application") as Application;
Run Code Online (Sandbox Code Playgroud)

Outlook和控制台应用程序都在我的用户帐户下运行,该帐户具有管理员权限.整个下午,我一直把头发拉出来.任何人都可以对此有所了解吗?干杯!

c# outlook marshalling console-application office-interop

0
推荐指数
1
解决办法
1136
查看次数

在没有Marshal.Copy或Unsafe的情况下在C ++中更新C#数组

我想更新在C#中创建的数组,然后将指向该数组的指针传递给C ++,然后让C ++填充索引,以供在C#中使用。现在,我正在使用Marshal.Copy()完成此任务,但我想避免可能不必要的复制,并返回c ++释放数组。这有可能吗?

这些数组是浮点数和整数,用于几何网格数据。

我当前的用法(正在工作,而不是我想使用的)C#

    IntPtr intptr=new IntPtr();
    int count = 0;
    PopulateArray(ref intptr, ref count);
    float[] resultVertices = new float[count];
    Marshal.Copy(intptr, resultVertices, 0, count);
Run Code Online (Sandbox Code Playgroud)

C ++

extern "C" __declspec(dllexport) bool PopulateArray(float** resultVerts, int* resultVertLength){

    *resultVerts = new float[5]{0.123f, 3.141529f, 127.001f, 42.42f, 0};
    int myX = 5;
    *resultVertLength = myX;
    return true;
}
Run Code Online (Sandbox Code Playgroud)

c# c++ marshalling

0
推荐指数
1
解决办法
2448
查看次数

Marshal返回我结构的空json

我正在编写一个代码,将一个库存扫描到一个结构中,以便将它导出到json.

目前,我的代码使用该ScanDir函数精确地扫描了一个库,但是当我尝试对我的结构进行Marshal时它只会返回{}.

// file's struct
type Fic struct {
    nom     string    `json:"fileName"`
    lon     int64     `json:"size"`
    tim     time.Time `json:"lastFileUpdate"`
    md5hash []byte    `json:"md5"`
}

// folder's struct
type Fol struct {
    subFol []Fol     `json:"listFolders"`
    files  []Fic     `json:"listFiles"`
    nom    string    `json:"folderName"`
    tim    time.Time `json:"lastFolderUpdate"`
}

func main() {
    var root Fol
    err := ScanDir("./folder", &root)   // scan a folder and fill my struct
    check(err)
    b, err := json.Marshal(root)
    check(err)
    os.Stdout.Write(b)
}

func check(err error) {
if err != nil …
Run Code Online (Sandbox Code Playgroud)

json marshalling go

0
推荐指数
1
解决办法
104
查看次数

从C#调用Delphi DLL并传递位图和字节数组

我试图用C#调用几个Delphi函数:

  MyType =array [1 .. 124] of byte
  procedure f(bytes: MyType); stdcall;
  external 'my.dll' name 'f';
Run Code Online (Sandbox Code Playgroud)

那是我的第一个问题.我试过了:

    [DllImport("Delphi/my.dll",
        CallingConvention = CallingConvention.StdCall,
        CharSet = CharSet.Auto)]
    public static extern
    void sygLadSyg([MarshalAs(UnmanagedType.LPArray)] byte[] myArray);
    void sygLadSyg([MarshalAs(UnmanagedType.SafeArray)] byte[] myArray); 
Run Code Online (Sandbox Code Playgroud)

我得到例外:

对PInvoke函数的调用使堆栈失衡.这很可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.

我究竟做错了什么?

第二个问题是传递位图.

function sygAnaliz(bitmapa: TBitmap): byte; stdcall;
  external 'awSygnat1.dll' name 'sygAnaliz';
 [DllImport("Delphi/awSygnat1.dll",
            CallingConvention = CallingConvention.StdCall,
            CharSet = CharSet.Ansi)]
        public static extern
        byte sygAnaliz(IntPtr bitmapPtr);
// and call itself
sygAnaliz(firstIMG.GetHbitmap());
Run Code Online (Sandbox Code Playgroud)

我得到异常:尝试读取或写入受保护的内存.这通常表明其他内存已损坏.

这两种功能都是内存安全的,因为它们已经使用了几年而效果很好.也许有一些我想念的东西?

c# delphi interop bitmap marshalling

0
推荐指数
1
解决办法
767
查看次数

如何在C#中固定Marshal.AllocHGlobal()分配的内存?

你如何固定Marshal.AllocHGlobal()分配的内存?

我的第一次尝试如下:

int bytes = 10;
IntPtr ip = Marshal.AllocHGlobal(bytes);
GCHandle iph = GCHandle.Alloc(ip, GCHandleType.Pinned);
Run Code Online (Sandbox Code Playgroud)

虽然我认为这只是引脚IntPtr而不是引用的内存块IntPtr.

.net c# pinvoke garbage-collection marshalling

0
推荐指数
1
解决办法
839
查看次数

为什么Go json.Marshal拒绝这些struct标签?json标签的正确语法是什么?

我正在尝试使用json.Marshal,但它拒绝接受我的struct标记。

我究竟做错了什么?

这是“ marshal.go”的源代码

https://play.golang.org/p/eFe03_89Ly9

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Name string `json: "name"`
    Age  int    `json: "age"`
}

func main() {
    p := Person{Name: "Alice", Age: 29}
    bytes, _ := json.Marshal(p)
    fmt.Println("JSON = ", string(bytes))
}
Run Code Online (Sandbox Code Playgroud)

我从“ go vet marshal.go”获得这些错误消息

./marshal.go:9: struct field tag `json: "name"` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
./marshal.go:10: struct field tag `json: "age"` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
Run Code Online (Sandbox Code Playgroud)

我在运行程序时得到此输出。 …

tags json struct marshalling go

0
推荐指数
1
解决办法
2357
查看次数