DLL里访问数组

CityHost 2002-11-25 06:53:21
我在做一个人口预测的过程中,需要使用皮尔曲线。我写了一个,如果在主程序里添加函数,自然可以访问主程序里的数组,因为我需要在大量的程序中使用,所以需要封装在DLL中,但如何编写函数的类型呢?
在主程序里的原型如下:
double per(double * Y,double T,double TT);
Y为一个一维数组的地址,T为数据年份,TT为预测年份
...全文
44 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
CityHost 2002-11-27
  • 打赏
  • 举报
回复
验证后结贴
dolphin2001 2002-11-25
  • 打赏
  • 举报
回复
创建共享数据段,
原文来自 community.borland.com
原文名字:Creating A Shared Memory Segment with C++Builder 4.0
译名: 在C++Builder中创建共享内存段
C++Builder 4.0 是第一个支持共享内存段的C++Builder 编译器。本文解释如何在windows DLL中使用此特性。

要改变数据段和类名字,您需要在您想要共享的文件中增加 #pragma option -zR[段名字]和#pragma option -zT[类名字]
下面的代码将输出一个称为'data'的整数。
File: SharedData.cpp
#pragma option -zRSHSEG // 改变缺省的数据段名字
#pragma option -zTSHCLASS // 改变缺省的数据类名字

// 初始化我们打算共享的数据
int data = 0;


注意本文件中的段名是SHSEGSHCLASS。
Linker需要一个.def文件来创建共享段。下面是.def文件的内容。

File: Shared.def
LIBRARY SHAREDDLL

SEGMENTS
SHSEG CLASS 'SHCLASS' SHARED


下面是包含dll入口点的dll源文件以及头文件。
包括了设置及取得共享内存段的方法。
File: SharedDLL.h
#ifdef __DLL__
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

extern "C"
{
void DLL_EXPORT SetData(int x);
int DLL_EXPORT GetData(void);
}

File: SharedDLL.cpp

#include
#pragma hdrstop

#include "Shared.h"
USEUNIT("SharedData.cpp");
USEDEF("Shared.def");

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}

extern int data;


void DLL_EXPORT SetData(int x)
{
data = x;
}

int DLL_EXPORT GetData(void)
{
return data;
}


SuperSuperLéon 2002-11-25
  • 打赏
  • 举报
回复
老哥,你去SDK/API问呀,解决的能快些。


呵呵。

用文件映射。

主要用到两个函数:

MapViewOfFile

CreateFileMapping


这是msdn的例子:


HANDLE hMapFile;

hMapFile = CreateFileMapping(hFile, // Current file handle.
NULL, // Default security.
PAGE_READWRITE, // Read/write permission.
0, // Max. object size.
0, // Size of hFile.
"MyFileMappingObject"); // Name of mapping object.

if (hMapFile == NULL)
{
ErrorHandler("Could not create file-mapping object.");
}
The process then uses the file-mapping object handle returned by CreateFileMapping in the call to MapViewOfFile to create a view of the file in the process's address space. The MapViewOfFile function returns a pointer to the file view.

LPVOID lpMapAddress;
lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
FILE_MAP_ALL_ACCESS, // Read/write permission
0, // Max. object size.
0, // Size of hFile.
0); // Map entire file.

if (lpMapAddress == NULL)
{
ErrorHandler("Could not map view of file.");
}
The second process calls the OpenFileMapping function with the name MyFileMappingObject to use the same file-mapping object as the first process. Like the first process, the second process uses the MapViewOfFile function to obtain a pointer to the file view.

HANDLE hMapFile;
LPVOID lpMapAddress;

hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission.
FALSE, // Do not inherit the name
"MyFileMappingObject"); // of the mapping object.

if (hMapFile == NULL)
{
ErrorHandler("Could not open file-mapping object.");
}

lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.
FILE_MAP_ALL_ACCESS, // Read/write permission.
0, // Max. object size.
0, // Size of hFile.
0); // Map entire file.

if (lpMapAddress == NULL)
{
ErrorHandler("Could not map view of file.");
}



阿,哈欠,我困了,我睡觉。bye.
kyodan 2002-11-25
  • 打赏
  • 举报
回复
SDK->跨进程数据共享
CityHost 2002-11-25
  • 打赏
  • 举报
回复
这里的问题是如果在主程序里调用,传递给Y的地址有意义,但在不同的的进程里访问,递给Y的地址是没有意义的。
bailingke 2002-11-25
  • 打赏
  • 举报
回复
和一般的函数没有什么不同
不过记着要在前面加一个delspec(dlloxport)

13,822

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧