Original Post
Basically i want to call some funcs i have written in c++ from c#. I think im putting together my dll correctly... but when my program try to PInvoke it it throws an exception saying it cant find the dll. The platform im trying to do this on is Pocket PC 2003. maybe there is something wrong with my code or technique. I compile the dll, and I've tried putting it in the windows directory and in my app's directory on the device (tried on a the emulator, I have ppc but havent seen if it works on device or not.). here is the dll's code: irdll.cpp, when i created this project in msvs2k5 I went to c++ smart device, then mfc dll then mfc shared dll(dynamically linked) here is irdll.h Ok and here is my C# project that attempts to use it Ir.cs and Form1.cs All of this compiles fine, I cant figure out why it wont load my dll. Do I need a full path for DllImport? if so how do i get the equivalent of App.Path in c#?
// irdll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "irdll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CirdllApp
BEGIN_MESSAGE_MAP(CirdllApp, CWinApp)
END_MESSAGE_MAP()
// CirdllApp construction
CirdllApp::CirdllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CirdllApp object
CirdllApp theApp;
// CirdllApp initialization
BOOL CirdllApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
UINT __cdecl FindPortIndex()
{
// Look into the registry for the IRDA port number
HKEY hKey = NULL;
//if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Drivers\\BuiltIn\\IrCOMM"), 0, 0, &hKey) == ERROR_SUCCESS)
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Comm\\IrDA"), 0, 0, &hKey) == ERROR_SUCCESS)
{
DWORD dwType = 0;
DWORD dwData = 0;
DWORD dwSize = sizeof(dwData);
if (RegQueryValueEx(hKey, _T("Port"), NULL, &dwType, (LPBYTE) &dwData, &dwSize) == ERROR_SUCCESS)
{
if (dwType == REG_DWORD && dwSize == sizeof(dwData))
{
RegCloseKey(hKey);
return (UINT) dwData;
}
}
RegCloseKey(hKey);
}
return 0;
}
// irdll.h : main header file for the irdll DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resourceppc.h"
#define DLLEXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C" {
#endif
DLLEXPORT UINT __cdecl FindPortIndex();
#ifdef __cplusplus
}
#endif
class CirdllApp : public CWinApp
{
public:
CirdllApp();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
namespace IrApp
{
class IR
{
[DllImport("irdll.dll")]
public static extern uint FindPortIndex();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace IrApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
uint i = IR.FindPortIndex();
}
}
}