Click here to Skip to main content
15,889,116 members
Home / Discussions / C#
   

C#

 
QuestionDataGridView Pin
DeepOceans16-Sep-07 23:51
DeepOceans16-Sep-07 23:51 
AnswerRe: DataGridView Pin
Giorgi Dalakishvili17-Sep-07 0:15
mentorGiorgi Dalakishvili17-Sep-07 0:15 
AnswerRe: DataGridView Pin
Imran Khan Pathan17-Sep-07 2:00
Imran Khan Pathan17-Sep-07 2:00 
QuestionSearch GUI to configurate Log4Net! Pin
T-Bear198716-Sep-07 23:25
T-Bear198716-Sep-07 23:25 
QuestionIntractivity with ms word?? Pin
Muammar©16-Sep-07 22:59
Muammar©16-Sep-07 22:59 
AnswerRe: Intractivity with ms word?? Pin
Pete O'Hanlon16-Sep-07 23:20
mvePete O'Hanlon16-Sep-07 23:20 
GeneralRe: Intractivity with ms word?? Pin
Muammar©17-Sep-07 0:55
Muammar©17-Sep-07 0:55 
QuestionWriting into an unmanaged buffer and marshalling it Pin
Leonardo Pelisoli16-Sep-07 22:46
Leonardo Pelisoli16-Sep-07 22:46 
Hello,

The code bellow shows a C# application loading an unmanaged C++ DLL. Note that the DLL uses a function pointer to call back one of the C# functions and pass arguments to it.

C# code
using System;
using System.Runtime.InteropServices;

namespace CsharpManaged
{
    class Program
    {
        public delegate void CallbackDelegate([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] String[] ar, int len);
        
        [DllImport("CppDLLUnmanaged.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void SetCallBackPointer(CallbackDelegate DelegatePassedToDLL);

        [DllImport("CppDLLUnmanaged.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void InvokeCallBack();

        static void Main(string[] args)
        {
            SetCallBackPointer(new CallbackDelegate(CallbackFunction));
            InvokeCallBack();

            Console.ReadLine();
        }

        private static void CallbackFunction(String[] ar, int len)
        {
            Console.WriteLine("I'm an array of " + ar.Length + " positions!");
            foreach(String element in ar)
            {
                Console.WriteLine(element);
            }
        }
    }
}


C++ code
typedef void __stdcall CALL_BACK_FUNCTION(char* ar[], int len);
CALL_BACK_FUNCTION *pFunction;

__declspec(dllexport) void __stdcall SetCallBackPointer(CALL_BACK_FUNCTION* FnAddress)
{
	pFunction = FnAddress;
}

__declspec(dllexport) void __stdcall InvokeCallBack()
{
	int len = 5;
	char** myArray = new char*[len];

	myArray[0] = "A";
	myArray[1] = "AB";
	myArray[2] = "ABC";
	myArray[3] = "ABCD";
	myArray[4] = "ABCDE";

	pFunction(myArray,len);

	delete [] myArray;
}


In the example I pass a string array from the DLL to the application. The contents arrive correctly, but I can't modify them.

What I need to do is add an extra argument in the call back function. Say, instead of
typedef void __stdcall CALL_BACK_FUNCTION(char* ar[], int len);
I want it to be
typedef void __stdcall CALL_BACK_FUNCTION(char* ar[], int len, char* result);
This extra argument is actually a return variable: I need the C# application to write something in it, and then the DLL should be able to use the data that was written.

I tried allocating the memory for this char* result buffer in the DLL, and then receiving it in the C# as a marshalled String. However, I'm not allowed to write in that String. Or rather I am, but the modifications don't take effect once I return to the DLL.

I'd appreciate any suggestions anyone might have in solving this problem.

Regards,
Leonardo
GeneralBump Pin
Leonardo Pelisoli19-Sep-07 11:59
Leonardo Pelisoli19-Sep-07 11:59 
AnswerRe: Writing into an unmanaged buffer and marshalling it Pin
Leonardo Pelisoli21-Sep-07 5:37
Leonardo Pelisoli21-Sep-07 5:37 
QuestionTo copy file from server..... Pin
P_Elza16-Sep-07 22:46
P_Elza16-Sep-07 22:46 
AnswerRe: To copy file from server..... Pin
Pete O'Hanlon16-Sep-07 23:17
mvePete O'Hanlon16-Sep-07 23:17 
QuestionRegarding Application Pool Pin
ngrj16-Sep-07 22:02
ngrj16-Sep-07 22:02 
AnswerRe: Regarding Application Pool Pin
Pete O'Hanlon17-Sep-07 1:45
mvePete O'Hanlon17-Sep-07 1:45 
GeneralRe: Regarding Application Pool Pin
ngrj17-Sep-07 17:05
ngrj17-Sep-07 17:05 
Questionstreamwriter Pin
troubled one16-Sep-07 21:56
troubled one16-Sep-07 21:56 
AnswerRe: streamwriter Pin
Pete O'Hanlon16-Sep-07 22:33
mvePete O'Hanlon16-Sep-07 22:33 
GeneralRe: streamwriter Pin
troubled one17-Sep-07 21:45
troubled one17-Sep-07 21:45 
AnswerRe: streamwriter Pin
N a v a n e e t h16-Sep-07 23:12
N a v a n e e t h16-Sep-07 23:12 
GeneralRe: streamwriter Pin
Giorgi Dalakishvili16-Sep-07 23:27
mentorGiorgi Dalakishvili16-Sep-07 23:27 
GeneralRe: streamwriter Pin
N a v a n e e t h16-Sep-07 23:37
N a v a n e e t h16-Sep-07 23:37 
AnswerRe: streamwriter Pin
blackjack215017-Sep-07 1:14
blackjack215017-Sep-07 1:14 
QuestionReducing the encrypted text size. Pin
M. J. Jaya Chitra16-Sep-07 21:33
M. J. Jaya Chitra16-Sep-07 21:33 
AnswerRe: Reducing the encrypted text size. Pin
Sathesh Sakthivel16-Sep-07 21:52
Sathesh Sakthivel16-Sep-07 21:52 
GeneralRe: Reducing the encrypted text size. Pin
M. J. Jaya Chitra16-Sep-07 22:13
M. J. Jaya Chitra16-Sep-07 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.