#include "malloc.h"
#include "winsock2.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>char p1[32];
char *pstIp=NULL;
typedef struct test
{
int data;
struct test *next;
}test_link;
test_link *head;int TestLink(test_link **ptest_head)
{
test_link *b;
test_link *a;
test_link *c;
test_link *d; a=(test_link*)malloc(sizeof(test_link));
b=(test_link*)malloc(sizeof(test_link)); a->data =1;
b->data = 2; c=(test_link*)malloc(sizeof(test_link));
d=(test_link*)malloc(sizeof(test_link)); c->data = 3;
d->data =4; a->next = b;
b->next =c;
c->next =d;
d->next =NULL;
head=a;
*ptest_head = head; return 0;
}void show(test_link *a)
{
test_link *p;
int j=0;
p=a;
do{
printf("%d\n",p->data);
p=p->next;
j++;
}while(p != NULL);
}
int main()
{ test_link *tt=(test_link*)malloc(sizeof(test_link));
int ret=0; ret = TestLink(&tt);
show(tt); free(tt);
return 0;
}
以上呢是用C写的一个简单的1,2,3,4组成的链表并读取出来。现在将这个链表封装成DLL后在delphi中调用,就不知道该怎么写了。请教各位能否用listbox将这个链表读取出来(其它手段都可以的)。

解决方案 »

  1.   


    test_link = Record
        int data;
        struct test *next;
    end;
    Ptest_link=^test_link;tt := new(Ptest_link);ret = TestLink(&tt);
    show(tt);//至于listbox将这个链表读取出来,修改Dll内部也可以地。
      

  2.   

    test_link = Record 
        data : Integer; 
        next : Ptest_link; 
    end; 
    Ptest_link=^test_link; 
    //调用
    tt := new(Ptest_link); ret = TestLink(&tt); 
    show(tt); //至于listbox将这个链表读取出来,修改Dll内部也可以地。 
      

  3.   

    请问一下这里的Ptest_link还需要在事件中定义吗?tt定义成string,ret定义成integer有错吗?
      

  4.   

    给楼主写了个
    DELPHI7 和 VS 2005编译  Ptestlink = ^Test1;
      Test1 =  record
        data: Integer;
        next: Ptestlink;
      end;
      
     var
      P : Ptestlink;
      TestFun: procedure (var test: Ptestlink); stdcall;
    begin
      New(p);  if H <> 0 then
        TestFun := GetProcAddress(H, 'GetLinkHead');  if Assigned(@TestFun) then
        TestFun(P);  Caption := IntToStr(p^.data);  while Assigned(p) do
      begin
        ShowMessage(IntToStr(p^.data));
        p := p.next;
      end;
    end; // testdll.cpp : 定义 DLL 应用程序的入口点。
    //#include "stdafx.h"
    #include "stdlib.h"
       #ifdef _MANAGED
    #pragma managed(push, off)
    #endifBOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
     )
    {
        return TRUE;
    }typedef struct test
    {
    int data;
    struct test *next;
    }*test_link;
    test_link head;int TestLink(test_link *ptest_head)
    {
    test_link b;
    test_link a;
    test_link c;
    test_link d; a=(test_link)malloc(sizeof(test));
    b=(test_link)malloc(sizeof(test)); a->data =1;
    b->data = 2; c=(test_link)malloc(sizeof(test));
    d=(test_link)malloc(sizeof(test)); c->data = 3;
    d->data =4; a->next = b;
    b->next =c;
    c->next =d;
    d->next =NULL;
    head=a; *ptest_head = head; return 0;
    }
    void __stdcall GetLinkHead(test_link* ptest_head)
    {
       TestLink(ptest_head);   
    }#ifdef _MANAGED
    #pragma managed(pop)
    #endif
    另外就是一个def 导出函数,你自己加个就可以