I have written these codes :
Mydll file :
Mydll.h
Expand|Select|Wrap|Line Numbers
- #ifndef MYDLL_H
- #define MYDLL_H
- #define EXPORT __declspec(dllexport)
- extern "C" void EXPORT __stdcall set(char c);
- extern "C" char EXPORT __stdcall get(void);
- #end
Mydll.cpp
Expand|Select|Wrap|Line Numbers
- #include "MyDll.h"
- #include <windows.h>
- extern "C" static char o ;
- void __stdcall set(char c)
- { o = c; }
- char __stdcall get(void)
- { return o; }
- BOOL WINAPI DllMain(HANDLE hModule,DWORD dwReason,LPVOID lpReserved)
- { return TRUE; }
Expand|Select|Wrap|Line Numbers
- LIBRARY "MyDll"
- EXPORTS
- set @1
- get @2
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace Csharp
- {
- class Program
- {
- [DllImport("Mydll.dll")]
- [return: MarshalAs(UnmanagedType.I1)]
- public static extern char get();
- [DllImport("Mydll.dll")]
- public static extern void set([MarshalAs(UnmanagedType.I1)]char value);
- static void Main(string[] args)
- {
- Console.WriteLine(get());
- Console.Read();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include<windows.h>
- main(){
- HANDLE ldll;
- typedef void (__stdcall *set)(char f);
- typedef char (__stdcall *get)(void);
- char k = 's' ;
- set Set;
- get Get;
- ldll = LoadLibrary(TEXT("MyDll.dll"));
- Set = (set)GetProcAddress(ldll, "set");
- Get = (get)GetProcAddress(ldll, "get");
- Set(k);
- printf("%c",Get());
- }
i get no errors but i get no result also !!!
when i call set and get in C program only i can see the char .
the same when i call them in C#.
but when i call set in C and get in C# so i can pass the char i get no output !!
i can't see the char !!