C# - call function from dynamic library dll

njz

I have to access dynamic library using C#. It works great when using COM library however when I try to use Dynamic library, it causing error.

1st problem

At first i do my code like this:

[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, char[] strVolume, char[] strInfo);
// strVolume and strInfo is parameter that return value with [out]

public static void Main()
{
char[] test1,test2;
toGetInfo(0,test1,test2);
}

But it unable to compile with error use of unassigned local variable for test1 and test2 . Then I edit my code by adding out like this:

[DllImport("mydll.dll")]
public static extern int toGetInfo(uint id, out char[] strVolume, out char[] strInfo);
// strVolume and strInfo is parameter that return [out]

public static void Main()
{
char[] test1,test2;
toGetInfo(0, out test1, out test2);
}

It able to compile but returns null value to test1 and test2.

2nd problem

[DllImport("mydll.dll")]
public static extern int toOpen(uint id, char* name);

 public static void Main()
{
 char name;
 toOpen(0, name);
}

When compile it give error "Pointers and fixed size buffers may only be used in an unsafe context"

Any idea how to do it?

jdweng

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication74
{
    class Program
    {
        [DllImport("mydll.dll")]
        public static extern int toGetInfo(uint id, IntPtr strVolume, IntPtr strInfo);

        [DllImport("mydll.dll")]
        public static extern int toOpen(uint id, IntPtr name);

        const int STRING_LENGTH = 256;
        static void Main(string[] args)
        {

            IntPtr strVolumePtr = Marshal.AllocHGlobal(STRING_LENGTH);
            IntPtr strInfoPtr = Marshal.AllocHGlobal(STRING_LENGTH);

            uint id = 123;

            int status1 = toGetInfo(id, strVolumePtr, strInfoPtr);

            string strVolume = Marshal.PtrToStringAnsi(strVolumePtr);
            string strInfo = Marshal.PtrToStringAnsi(strInfoPtr);

            string name = "John";
            IntPtr openNamePtr = Marshal.StringToHGlobalAnsi(name);
            int status2 = toOpen(id, openNamePtr);

            Marshal.FreeHGlobal(strVolumePtr);
            Marshal.FreeHGlobal(strInfoPtr);
            Marshal.FreeHGlobal(openNamePtr);

        }

    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

Go: Function call from a library

From Dev

Call C standard library function from asm in Visual Studio

From Dev

Troubleshooting DLL function call from Excel Vba

From Dev

Call function with default argument from .dll with c++

From Dev

How to call a C++ dynamic library from Rust?

From Dev

Call C++ DLL function in Delphi 7

From Dev

Call a member function with offset address from Dll

From Dev

Call C dll from Fortran

From Dev

Load a dynamic shared library (DLL) on Mac in C++ using CFBundleCreate

From Dev

Call C++ function from inside dynamically loaded dll

From Dev

How can I call a function from C# app in a C++/CLI DLL?

From Dev

python call function from DLL

From Dev

Creating dll library from generated c code

From Dev

C# dynamic dll call gettype

From Dev

How to call function in my dll from jquery

From Dev

Can We Call C# Library dll from Apache Cordova?

From Dev

C++ How to get the right return from dll function call

From Dev

How to call a C++ DLL function from C# project on Windows Phone

From Dev

Why is this call to a dynamic library function so slow?

From Dev

Call .dll function from C# Ironpython

From Dev

How can i call JNIEXPORT function from dll in other c++ dll

From Dev

Not able to call the function from C DLL

From Dev

How to call static library static class member function from victim process of dll-injection process

From Dev

C#: A dynamic link library (DLL) initialization routine failed

From Dev

Call function from android library

From Dev

Call function in C++ dll from C#

From Dev

Call C dll function from C#

From Dev

Can not call a function from a C# DLL

From Dev

Call C# dll function from TypeScript | Overwolf

Related Related

HotTag

Archive